Godot Version
Godot Version 4.1.2.stable.mono
Question
So I made a game for a school project and now I want to connect the game to a mySQL database. My problem is that the HTTPRequest doesn’t work. The game is a space shooter game where you can shot ufos that come from above and for every destroyed ufo you get a score. My idea was that I want the user to be able to login into a account that you can make on a website and that make a leaderboard on the website. For that I need to send the highscore to the database. For testing purposes I used XAMPP to host locally. I’ve read that you can’t directly connect your Game to a database, so you have to have something in between. That’s why I made a login.php file. This is my first time posting something to a forum so I’ll just put the GDScript and the php code in here.
GDScript:
extends Node2D
@onready var http_request
const url = “http://localhost:80/login.php”
var request : HTTPRequest
func _ready():
http_request = HTTPRequest.new()
print(“Connecting signal…”)
http_request.connect(“request_completed”, self, “_on_request_completed”)
print(“Signal connected.”)
http_request.request_timeout = 10
http_request.request_follow_redirects = true
http_request.request_max_redirects = 5
http_request.request_use_ssl = false
http_request.request_max_body_size = 1024 * 1024
add_child(http_request)
func _on_login_button_pressed():
var username = $Username_Field.text
var password = $Password_Field.text
var params = {“usernames”: username, “passwords”: password}
print("Sending data: ", {“usernames”: username, “passwords”: password})
http_request.request(url, params)
#Add the signal handling function
func _on_request_completed(result, response_code, headers, body):
if response_code == 200:
if body == “success”:
# Successful login
# You can now access the highscore or perform other actions
print(“Login successful!”)
else:
# Failed login
print(“Login failed. Invalid username or password.”)
else:
print("HTTP request failed with code: ", response_code)
func _on_back_button_pressed():
get_tree().change_scene_to_file(“res://scenes/start_screen.tscn”)
PHP:
<?php // login.php // Debugging $servername = "localhost"; $username = "root"; // Default XAMPP username $password = ""; // Default XAMPP password $dbname = "lf8projekt_jefcompany"; // Replace with your actual database name // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // Check if 'username' and 'password' keys exist in the POST data if(isset($_POST['username']) && isset($_POST['password'])){ $username = $_POST['username']; $pass = $_POST['password']; // Fetch the hashed password from the database $query = "SELECT usernames, passwords FROM users WHERE usernames='$username'"; $result = mysqli_query($conn, $query); if (mysqli_num_rows($result) > 0) { $user_data = mysqli_fetch_assoc($result); $hashedPassword = $user_data['passwords']; // Verify the entered password with the hashed password if (password_verify($pass, $hashedPassword)) { echo "success"; } else { echo "failure"; } } else { echo "failure"; } } else { echo "Invalid data received from the client."; } $conn->close(); ?>It looks pretty weird in the preview, so I’ll just include a workupload link to both the game and the login.php file: workupload - Are you a human? . If there are any questions that feel free to ask me.
Thank you in advance