Unity Login System med PHP og MySQL
I denne tutorial vil jeg vise, hvordan man opretter et login-system i Unity ved hjælp af PHP og MySQL.
Denne vejledning kræver en server med cPanel sammen med PHP og MySQLi (en forbedret version af MySQL).
Du er velkommen til at tjekke den overkommelige premium VPS hosting eller et billigere Shared Hosting alternativ.
Trin 1: Konfigurer MySQL-database
- Log ind på cPanel
- Klik på MySQL-databaser i afsnittet DATABASER
- Indtast navnet på din database under "Create New Database"-afsnittet og klik derefter "Create Database"
Efter at databasen er oprettet, skal vi oprette en bruger, der vil blive knyttet til den:
- Indtast brugernavnet under "MySQL Users" sektionen og indtast derefter adgangskoden (eller brug helst adgangskodegeneratoren til at generere en stærk adgangskode). Glem ikke at gemme adgangskoden et sted, den bliver nødvendig senere.
Til sidst skal vi tildele en bruger til en database med et specifikt sæt tilladelser.
- Under "Add User To Database" sektionen skal du vælge den nyoprettede bruger og database og derefter klikke "Add"
Efter at have klikket på "Add" vil du blive præsenteret for en liste over privilegier. Vælg kun de privilegier, som du planlægger at bruge i fremtiden. De mest almindelige er DELETE, SELECT, INSERT og UPDATE:
- Klik på "Make Changes" for at afslutte
Trin 2: Opret MySQL-tabel
MySQL-tabel gemmer de værdier, som brugerne har angivet, såsom brugernavn, e-mail, adgangskode osv.
- Klik på "phpMyAdmin" under DATABASER sektionen
- Klik på den nyoprettede database og klik derefter på SQL-fanen
- Indsæt koden nedenfor i forespørgselseditoren og klik derefter "Go"
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
--
-- Table structure for table `sc_users`
--
CREATE TABLE `sc_users` (
`user_id` int(11) NOT NULL,
`username` varchar(20) CHARACTER SET utf8 NOT NULL,
`email` varchar(254) CHARACTER SET utf8 NOT NULL,
`password` char(60) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`registration_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Indexes for table `sc_users`
--
ALTER TABLE `sc_users`
ADD PRIMARY KEY (`user_id`),
ADD UNIQUE KEY `username` (`username`),
ADD UNIQUE KEY `email` (`email`);
--
-- AUTO_INCREMENT for table `sc_users`
--
ALTER TABLE `sc_users`
MODIFY `user_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;
COMMIT;
Forespørgslen ovenfor vil oprette en ny tabel 'sc_users', som gemmer brugerdataene.
Trin 3: Programmer serversidelogik
Server-side logik vil bestå af PHP scripts, der vil modtage og behandle post data fra Unity.
Den første fil er database.php, som vil oprette forbindelse til MySQL-databasen ved hjælp af PHP MySQLi-udvidelsen.
database.php
<?php
$host = "localhost"; // Host name
$db_username = "DATABASE_USER"; // Mysql username
$db_password = "USER_PASSWORD"; // Mysql password
$db_name = "DATABASE_NAME"; // Database name
$mysqli_conection = mysqli_connect($host, $db_username, $db_password, $db_name)or die("cannot connect");
?>
Erstat DATABASE_USER, USER_PASSWORD, og DATABASE_NAME med dine værdier.
Den anden fil er register.php. Denne fil vil modtage postdata fra Unity og indsætte en ny brugerpost i en databasetabel.
register.php
<?php
if(isset($_POST["email"]) && isset($_POST["username"]) && isset($_POST["password1"]) && isset($_POST["password2"])){
$errors = array();
$emailMaxLength = 254;
$usernameMaxLength = 20;
$usernameMinLength = 3;
$passwordMaxLength = 19;
$passwordMinLength = 5;
$email = strtolower($_POST["email"]);
$username = $_POST["username"];
$password1 = $_POST["password1"];
$password2 = $_POST["password2"];
//Validate email
if(preg_match('/\s/', $email)){
$errors[] = "Email can't have spaces";
}else{
if(!validate_email_address($email)){
$errors[] = "Invalid email";
}else{
if(strlen($email) > $emailMaxLength){
$errors[] = "Email is too long, must be equal or under " . strval($emailMaxLength) . " characters";
}
}
}
//Validate username
if(strlen($username) > $usernameMaxLength || strlen($username) < $usernameMinLength){
$errors[] = "Incorrect username length, must be between " . strval($usernameMinLength) . " and " . strval($usernameMaxLength) . " characters";
}else{
if(!ctype_alnum ($username)){
$errors[] = "Username must be alphanumeric";
}
}
//Validate password
if($password1 != $password2){
$errors[] = "Passwords do not match";
}else{
if(preg_match('/\s/', $password1)){
$errors[] = "Password can't have spaces";
}else{
if(strlen($password1) > $passwordMaxLength || strlen($password1) < $passwordMinLength){
$errors[] = "Incorrect password length, must be between " . strval($passwordMinLength) . " and " . strval($passwordMaxLength) . " characters";
}else{
if(!preg_match('/[A-Za-z]/', $password1) || !preg_match('/[0-9]/', $password1)){
$errors[] = "Password must contain atleast 1 letter and 1 number";
}
}
}
}
//Check if there is user already registered with the same email or username
if(count($errors) == 0){
//Connect to database
require dirname(__FILE__) . '/database.php';
if ($stmt = $mysqli_conection->prepare("SELECT username, email FROM sc_users WHERE email = ? OR username = ? LIMIT 1")) {
/* bind parameters for markers */
$stmt->bind_param('ss', $email, $username);
/* execute query */
if($stmt->execute()){
/* store result */
$stmt->store_result();
if($stmt->num_rows > 0){
/* bind result variables */
$stmt->bind_result($username_tmp, $email_tmp);
/* fetch value */
$stmt->fetch();
if($email_tmp == $email){
$errors[] = "User with this email already exist.";
}
else if($username_tmp == $username){
$errors[] = "User with this name already exist.";
}
}
/* close statement */
$stmt->close();
}else{
$errors[] = "Something went wrong, please try again.";
}
}else{
$errors[] = "Something went wrong, please try again.";
}
}
//Finalize registration
if(count($errors) == 0){
$hashedPassword = password_hash($password1, PASSWORD_BCRYPT);
if ($stmt = $mysqli_conection->prepare("INSERT INTO sc_users (username, email, password) VALUES(?, ?, ?)")) {
/* bind parameters for markers */
$stmt->bind_param('sss', $username, $email, $hashedPassword);
/* execute query */
if($stmt->execute()){
/* close statement */
$stmt->close();
}else{
$errors[] = "Something went wrong, please try again.";
}
}else{
$errors[] = "Something went wrong, please try again.";
}
}
if(count($errors) > 0){
echo $errors[0];
}else{
echo "Success";
}
}else{
echo "Missing data";
}
function validate_email_address($email) {
return preg_match('/^([a-z0-9!#$%&\'*+-\/=?^_`{|}~.]+@[a-z0-9.-]+\.[a-z0-9]+)$/i', $email);
}
?>
Og den sidste fil er login.php, som vil modtage legitimationsoplysningerne og kontrollere dem mod sc_users-tabellen.
login.php
<?php
if(isset($_POST["email"]) && isset($_POST["password"])){
$errors = array();
$email = $_POST["email"];
$password = $_POST["password"];
//Connect to database
require dirname(__FILE__) . '/database.php';
if ($stmt = $mysqli_conection->prepare("SELECT username, email, password FROM sc_users WHERE email = ? LIMIT 1")) {
/* bind parameters for markers */
$stmt->bind_param('s', $email);
/* execute query */
if($stmt->execute()){
/* store result */
$stmt->store_result();
if($stmt->num_rows > 0){
/* bind result variables */
$stmt->bind_result($username_tmp, $email_tmp, $password_hash);
/* fetch value */
$stmt->fetch();
if(password_verify ($password, $password_hash)){
echo "Success" . "|" . $username_tmp . "|" . $email_tmp;
return;
}else{
$errors[] = "Wrong email or password.";
}
}else{
$errors[] = "Wrong email or password.";
}
/* close statement */
$stmt->close();
}else{
$errors[] = "Something went wrong, please try again.";
}
}else{
$errors[] = "Something went wrong, please try again.";
}
if(count($errors) > 0){
echo $errors[0];
}
}else{
echo "Missing data";
}
?>
- Upload alle 3 filer til din public_html mappe.
Trin 4: Programmer klientlogik ind Unity
Klientlogik vil bestå af et C#-script, der sender data til et PHP-script.
- Opret et nyt script i Unity og navngiv det SC_LoginSystem og indsæt derefter koden nedenfor i det:
SC_LoginSystem.cs
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
public class SC_LoginSystem : MonoBehaviour
{
public enum CurrentWindow { Login, Register }
public CurrentWindow currentWindow = CurrentWindow.Login;
string loginEmail = "";
string loginPassword = "";
string registerEmail = "";
string registerPassword1 = "";
string registerPassword2 = "";
string registerUsername = "";
string errorMessage = "";
bool isWorking = false;
bool registrationCompleted = false;
bool isLoggedIn = false;
//Logged-in user data
string userName = "";
string userEmail = "";
string rootURL = "http://YOUR_SITE.COM/"; //Path where php files are located
void OnGUI()
{
if (!isLoggedIn)
{
if (currentWindow == CurrentWindow.Login)
{
GUI.Window(0, new Rect(Screen.width / 2 - 125, Screen.height / 2 - 115, 250, 230), LoginWindow, "Login");
}
if (currentWindow == CurrentWindow.Register)
{
GUI.Window(0, new Rect(Screen.width / 2 - 125, Screen.height / 2 - 165, 250, 330), RegisterWindow, "Register");
}
}
GUI.Label(new Rect(5, 5, 500, 25), "Status: " + (isLoggedIn ? "Logged-in Username: " + userName + " Email: " + userEmail : "Logged-out"));
if (isLoggedIn)
{
if (GUI.Button(new Rect(5, 30, 100, 25), "Log Out"))
{
isLoggedIn = false;
userName = "";
userEmail = "";
currentWindow = CurrentWindow.Login;
}
}
}
void LoginWindow(int index)
{
if (isWorking)
{
GUI.enabled = false;
}
if (errorMessage != "")
{
GUI.color = Color.red;
GUILayout.Label(errorMessage);
}
if (registrationCompleted)
{
GUI.color = Color.green;
GUILayout.Label("Registration Completed!");
}
GUI.color = Color.white;
GUILayout.Label("Email:");
loginEmail = GUILayout.TextField(loginEmail);
GUILayout.Label("Password:");
loginPassword = GUILayout.PasswordField(loginPassword, '*');
GUILayout.Space(5);
if (GUILayout.Button("Submit", GUILayout.Width(85)))
{
StartCoroutine(LoginEnumerator());
}
GUILayout.FlexibleSpace();
GUILayout.Label("Do not have account?");
if (GUILayout.Button("Register", GUILayout.Width(125)))
{
ResetValues();
currentWindow = CurrentWindow.Register;
}
}
void RegisterWindow(int index)
{
if (isWorking)
{
GUI.enabled = false;
}
if (errorMessage != "")
{
GUI.color = Color.red;
GUILayout.Label(errorMessage);
}
GUI.color = Color.white;
GUILayout.Label("Email:");
registerEmail = GUILayout.TextField(registerEmail, 254);
GUILayout.Label("Username:");
registerUsername = GUILayout.TextField(registerUsername, 20);
GUILayout.Label("Password:");
registerPassword1 = GUILayout.PasswordField(registerPassword1, '*', 19);
GUILayout.Label("Password Again:");
registerPassword2 = GUILayout.PasswordField(registerPassword2, '*', 19);
GUILayout.Space(5);
if (GUILayout.Button("Submit", GUILayout.Width(85)))
{
StartCoroutine(RegisterEnumerator());
}
GUILayout.FlexibleSpace();
GUILayout.Label("Already have an account?");
if (GUILayout.Button("Login", GUILayout.Width(125)))
{
ResetValues();
currentWindow = CurrentWindow.Login;
}
}
IEnumerator RegisterEnumerator()
{
isWorking = true;
registrationCompleted = false;
errorMessage = "";
WWWForm form = new WWWForm();
form.AddField("email", registerEmail);
form.AddField("username", registerUsername);
form.AddField("password1", registerPassword1);
form.AddField("password2", registerPassword2);
using (UnityWebRequest www = UnityWebRequest.Post(rootURL + "register.php", form))
{
yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success)
{
errorMessage = www.error;
}
else
{
string responseText = www.downloadHandler.text;
if (responseText.StartsWith("Success"))
{
ResetValues();
registrationCompleted = true;
currentWindow = CurrentWindow.Login;
}
else
{
errorMessage = responseText;
}
}
}
isWorking = false;
}
IEnumerator LoginEnumerator()
{
isWorking = true;
registrationCompleted = false;
errorMessage = "";
WWWForm form = new WWWForm();
form.AddField("email", loginEmail);
form.AddField("password", loginPassword);
using (UnityWebRequest www = UnityWebRequest.Post(rootURL + "login.php", form))
{
yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success)
{
errorMessage = www.error;
}
else
{
string responseText = www.downloadHandler.text;
if (responseText.StartsWith("Success"))
{
string[] dataChunks = responseText.Split('|');
userName = dataChunks[1];
userEmail = dataChunks[2];
isLoggedIn = true;
ResetValues();
}
else
{
errorMessage = responseText;
}
}
}
isWorking = false;
}
void ResetValues()
{
errorMessage = "";
loginEmail = "";
loginPassword = "";
registerEmail = "";
registerPassword1 = "";
registerPassword2 = "";
registerUsername = "";
}
}
Erstat http://DIN_SITE.COM/ med din værdi (Dette bør være en rodsti, hvor du uploadede PHP-filer). Sørg også for at bruge HTTPS, hvis din server har et SSL-certifikat installeret.
- Opret et nyt GameObject og navngiv det "LoginSystem"
- Vedhæft til SC_LoginSystem til LoginSystem Object
Tryk på Afspil, og klik derefter på Registrer for at oprette en ny konto eller angive legitimationsoplysninger for at logge ind.
Vi har en fortsættelse af denne tutorial om, hvordan man opretter en online leaderboard.