Login and Registration System

This is a Branch of Xandeck’s tutorial.

For the login part go here: http://forum.unity3d.com/threads/24721-Tutorial-Unity-and-PHP-login-script-simple-but-useful

Registration:

=== Create the PHP file called: check_scoresR.php
Code:

<?
// CONNECTIONS =========================================================
$host = "localhost"; //put your host here
$user = "myuser"; //in general is root
$password = "mypassword"; //use your password here
$dbname = "mydatabase"; //your database
mysql_connect($host, $user, $password) or die("Cant connect into database1");
mysql_select_db($dbname)or die("Cant connect into database");
// =============================================================================
$unityHash = $_POST["myform_hash"];
$phpHash = "hashcode"; // same code in here as in your Unity game

$nick = $_POST["myform_nick"]; 
$pass = md5($_POST["myform_pass"]); // md5 hashes the password

if(!$nick || !$pass) 
{
    echo "Login or password cant be empty.";
} 
else 
{
    if ($unityHash != $phpHash)
	{
        echo "HASH code is diferent from your game, you infidel.";
    } 
	else 
	{
		$checkuser = mysql_query("SELECT name FROM scores WHERE name='$nick'"); 
        $username_exist = mysql_num_rows($checkuser);
		if($username_exist > 0)
		{
			  echo "I'm sorry but the username you specified has already been taken.  Please pick another one.";
			  unset($nick);
			  exit();
		}
		if($username_exist = 0)
		{
			$query = "INSERT INTO scores (name, password)
			VALUES('$nick', '$pass')";
			mysql_query($query) or die(mysql_error());
			mysql_close();
			echo "You have successfully Registered";
			
		}
       
    }
}

Create a new Javascript file and change the name for whatever you want, here I will use phpUnityR.
Put this code on:

Code

private var formNick = ""; //this is the field where the player will put the name to login
private var formPassword = ""; //this is his password
var formText = ""; //this field is where the messages sent by PHP script will be in

var URL = "http://yoursite.com/check_scoresR.php"; //change for your URL
var hash = "hashcode"; //change your secret code, and remember to change into the PHP file too

private var textrect = Rect (10, 150, 500, 500); //just make a GUI object rectangle

function OnGUI() {
	GUI.Label( Rect (10, 10, 80, 20), "Your nick:" ); //text with your nick
	GUI.Label( Rect (10, 30, 80, 20), "Your pass:" );

	formNick = GUI.TextField ( Rect (90, 10, 100, 20), formNick ); //here you will insert the new value to variable formNick
	formPassword = GUI.TextField ( Rect (90, 30, 100, 20), formPassword ); //same as above, but for password

	if ( GUI.Button ( Rect (10, 60, 100, 20) , "Register" ) ){ //just a button
		Register();
	}
	GUI.TextArea( textrect, formText );
}

function Register() {
	var form = new WWWForm(); //here you create a new form connection
	form.AddField( "myform_hash", hash ); //add your hash code to the field myform_hash, check that this variable name is the same as in PHP file
	form.AddField( "myform_nick", formNick );
	form.AddField( "myform_pass", formPassword );
	var w = WWW(URL, form); //here we create a var called 'w' and we sync with our URL and the form
	yield w; //we wait for the form to check the PHP file, so our game dont just hang
	if (w.error != null) {
		print(w.error); //if there is an error, tell us
	} else {
		print("Test ok");
		formText = w.data; //here we return the data our PHP told us
		w.Dispose(); //clear our form in game
	}

	formNick = ""; //just clean our variables
	formPassword = "";
}

Put this code inside a game object, or even your Main camera. Just HIT play and it must work
Now, use your imagination and make your game insert data into your database, or return more data, etc.

I have a problem with the register, I copied the scripts and everything, i didn’t touched anything!, But when i’m putting my nickname and my password and clicking “Register” the coloumns of nickname and password became empty and that’s it…

i have that and the next thing im trying to do is what comes after login anyone help? answer here : http://answers.unity3d.com/questions/583268/how-to-get-the-player-name-level-and-money-out-of.html

Everything is working except it doesnt say You have successfully Registered and it doesnt put it into the database.