Skip to content

ThirteeNov

My personal blog about coding and internet

Menu
  • About me
  • About Zofia Kreasi
  • Cart
  • Checkout
  • Making an airplane game from scratch in Unity
  • My account
  • Privacy Policy
  • Privacy Policy – zkLeaderboard
  • Sample Page
  • Shop
  • Tutorials on Learning JavaScript
  • ZKAccounts – Privacy Policy
Menu

How to make Unity & PHP Login Logout and Register

Posted on October 16, 2020November 10, 2020 by Habibie

Hello guys! Watch this series of my videos to see how we can make Unity login, logout and register easily:

And here are the code I wrote:

First, config.php file

<?php

$databasename = "mydatabase";
$host = "localhost";
$dbuser = "root";
$dbpassword = "";
$connection = mysqli_connect($host, $dbuser, $dbpassword, $databasename);

?>

Then the index.php backend file

<?php

include("config.php");

if(isset($_POST["newAccountUsername"])){
	$username = mysqli_real_escape_string($connection, $_POST["newAccountUsername"]);
	$password = mysqli_real_escape_string($connection, $_POST["newAccountPassword"]);
	//Check are they empty?
	if($username != "" && $password != ""){
		//Check is the username has not taken
		if(mysqli_num_rows(mysqli_query($connection, "SELECT * FROM unity_users WHERE username = '$username'")) == 0){
			mysqli_query($connection, "INSERT INTO unity_users (username, password) VALUES ('$username', '$password')");
			echo "Regitering new account: Username " . $username . " and password: " . $password;
		}else{
			echo "This Username is not available. Please use another username.";
		}		
	}else{
		echo "Both fields are required.";
	}
}else if(isset($_POST["loginUsername"])){
	$username = mysqli_real_escape_string($connection, $_POST["loginUsername"]);
	$password = mysqli_real_escape_string($connection, $_POST["loginPassword"]);
	if($username != "" && $password != ""){
		//Check are entered username and password matched
		$sql = "SELECT * FROM unity_users WHERE username = '$username' AND password = '$password'";
		if(mysqli_num_rows(mysqli_query($connection, $sql)) > 0){
			echo 1;
		}else{
			echo 0;
		}
	}else{
		echo "Both fields are required.";
	}
}else{
	echo "Unity Login Logout and Register";
}

?>

And the last file is UnityLoginLogoutRegister.cs file

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;

public class UnityLoginLogoutRegister : MonoBehaviour
{
	
	public string baseUrl = "http://localhost/ThirteeNov/UnityLoginLogoutRegister/";
	
	
	public InputField accountUserName;
	public InputField accountPassword;
	public Text info;
	
	private string currentUsername;
	private string ukey = "accountusername";
	
    // Start is called before the first frame update
    void Start()
    {
        currentUsername = "";
		
		if(PlayerPrefs.HasKey(ukey)){
			if(PlayerPrefs.GetString(ukey) != ""){
				currentUsername = PlayerPrefs.GetString(ukey);
				info.text = "You are loged in = " + currentUsername;
			}else{
				info.text = "You are not loged in.";
			}
		}else{
			info.text = "You are not loged in.";
		}
		
    }

    // Update is called once per frame
    void Update()
    {
        
    }
	
	public void AccountLogout(){
		currentUsername = "";
		PlayerPrefs.SetString(ukey, currentUsername);
		info.text = "You are just loged out.";
	}
	
	public void AccountRegister()
    {
        string uName = accountUserName.text;
        string pWord = accountPassword.text;
        StartCoroutine(RegisterNewAccount(uName, pWord));
    }
	
	public void AccountLogin()
    {
        string uName = accountUserName.text;
        string pWord = accountPassword.text;
        StartCoroutine(LogInAccount(uName, pWord));
    }
	
	IEnumerator RegisterNewAccount(string uName, string pWord)
    {
        WWWForm form = new WWWForm();
        form.AddField("newAccountUsername", uName);
        form.AddField("newAccountPassword", pWord);
        using (UnityWebRequest www = UnityWebRequest.Post(baseUrl, form))
        {
            www.downloadHandler = new DownloadHandlerBuffer();
            yield return www.SendWebRequest();
 
            if (www.isNetworkError)
            {
                Debug.Log(www.error);
            }
            else
            {
                string responseText = www.downloadHandler.text;
                Debug.Log("Response = " + responseText);
                info.text = "Response = " + responseText;
            }
        }
    }
	
	IEnumerator LogInAccount(string uName, string pWord)
    {
        WWWForm form = new WWWForm();
        form.AddField("loginUsername", uName);
        form.AddField("loginPassword", pWord);
        using (UnityWebRequest www = UnityWebRequest.Post(baseUrl, form))
        {
            www.downloadHandler = new DownloadHandlerBuffer();
            yield return www.SendWebRequest();
 
            if (www.isNetworkError)
            {
                Debug.Log(www.error);
            }
            else
            {
                string responseText = www.downloadHandler.text;
				if(responseText == "1"){
					PlayerPrefs.SetString(ukey, uName);
					//info.text = "Login Success with username " + uName;
					SceneManager.LoadSceneAsync("Home");
				}else{
					info.text = "Login Failed.";
				}
            }
        }
    }
}

In case you have problem seeing above scripts, check out this github link: https://github.com/habibieamrullah/SimpleUnityLoginLogoutRegisterSystem

Post Views: 1,679
ciihuy2020

Welcome!

  • My YouTube Channel
  • My GitHub Page
  • About me

Categories

  • 3DVista
  • Android
  • Apache
  • C#
  • Cordova
  • Electron & Node JS
  • HTML5, CSS & JavaScript
  • iOS
  • Let's Make Unity Games
  • Misc
  • Photoshop
  • PHP
  • Python
  • Uncategorized
  • Unity
  • WordPress

Recent Posts

  • Hover Reveal script for Unity to show and hide object on mouse hover
  • How to Prevent UI Clicks in Unity from “Bleeding Through” to 3D Objects Behind Them
  • Make objects like wires and cables easily in Unity using Ciihuy Curved Mesh
  • [SOLVED] Can’t Add Custom Domain to Blogger After Losing CNAME Verification
  • iOS App Icon Generator by CiihuyCom
© 2026 ThirteeNov | Powered by Superbs Personal Blog theme