Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Login and Registration System

Discussion in 'Multiplayer' started by brandon199511, Jan 19, 2012.

  1. brandon199511

    brandon199511

    Joined:
    Sep 21, 2010
    Posts:
    268
    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:
    Code (csharp):
    1.  
    2. <?
    3. // CONNECTIONS =========================================================
    4. $host = "localhost"; //put your host here
    5. $user = "myuser"; //in general is root
    6. $password = "mypassword"; //use your password here
    7. $dbname = "mydatabase"; //your database
    8. mysql_connect($host, $user, $password) or die("Cant connect into database1");
    9. mysql_select_db($dbname)or die("Cant connect into database");
    10. // =============================================================================
    11. $unityHash = $_POST["myform_hash"];
    12. $phpHash = "hashcode"; // same code in here as in your Unity game
    13.  
    14. $nick = $_POST["myform_nick"];
    15. $pass = md5($_POST["myform_pass"]); // md5 hashes the password
    16.  
    17. if(!$nick || !$pass)
    18. {
    19.     echo "Login or password cant be empty.";
    20. }
    21. else
    22. {
    23.     if ($unityHash != $phpHash)
    24.     {
    25.         echo "HASH code is diferent from your game, you infidel.";
    26.     }
    27.     else
    28.     {
    29.         $checkuser = mysql_query("SELECT name FROM scores WHERE name='$nick'");
    30.         $username_exist = mysql_num_rows($checkuser);
    31.         if($username_exist > 0)
    32.         {
    33.               echo "I'm sorry but the username you specified has already been taken.  Please pick another one.";
    34.               unset($nick);
    35.               exit();
    36.         }
    37.         if($username_exist = 0)
    38.         {
    39.             $query = "INSERT INTO scores (name, password)
    40.             VALUES('$nick', '$pass')";
    41.             mysql_query($query) or die(mysql_error());
    42.             mysql_close();
    43.             echo "You have successfully Registered";
    44.            
    45.         }
    46.        
    47.     }
    48. }
    49.  
    Create a new Javascript file and change the name for whatever you want, here I will use phpUnityR.
    Put this code on:

    Code
    Code (csharp):
    1.  
    2. private var formNick = ""; //this is the field where the player will put the name to login
    3. private var formPassword = ""; //this is his password
    4. var formText = ""; //this field is where the messages sent by PHP script will be in
    5.  
    6. var URL = "http://yoursite.com/check_scoresR.php"; //change for your URL
    7. var hash = "hashcode"; //change your secret code, and remember to change into the PHP file too
    8.  
    9. private var textrect = Rect (10, 150, 500, 500); //just make a GUI object rectangle
    10.  
    11. function OnGUI() {
    12.     GUI.Label( Rect (10, 10, 80, 20), "Your nick:" ); //text with your nick
    13.     GUI.Label( Rect (10, 30, 80, 20), "Your pass:" );
    14.  
    15.     formNick = GUI.TextField ( Rect (90, 10, 100, 20), formNick ); //here you will insert the new value to variable formNick
    16.     formPassword = GUI.TextField ( Rect (90, 30, 100, 20), formPassword ); //same as above, but for password
    17.  
    18.     if ( GUI.Button ( Rect (10, 60, 100, 20) , "Register" ) ){ //just a button
    19.         Register();
    20.     }
    21.     GUI.TextArea( textrect, formText );
    22. }
    23.  
    24. function Register() {
    25.     var form = new WWWForm(); //here you create a new form connection
    26.     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
    27.     form.AddField( "myform_nick", formNick );
    28.     form.AddField( "myform_pass", formPassword );
    29.     var w = WWW(URL, form); //here we create a var called 'w' and we sync with our URL and the form
    30.     yield w; //we wait for the form to check the PHP file, so our game dont just hang
    31.     if (w.error != null) {
    32.         print(w.error); //if there is an error, tell us
    33.     } else {
    34.         print("Test ok");
    35.         formText = w.data; //here we return the data our PHP told us
    36.         w.Dispose(); //clear our form in game
    37.     }
    38.  
    39.     formNick = ""; //just clean our variables
    40.     formPassword = "";
    41. }
    42.  
    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.
     
    Last edited: Jan 19, 2012
  2. 3DRtDE

    3DRtDE

    Joined:
    Aug 7, 2011
    Posts:
    15
    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..
     
  3. mbivol10

    mbivol10

    Joined:
    Nov 6, 2013
    Posts:
    3
  4. theFluff

    theFluff

    Joined:
    Dec 1, 2013
    Posts:
    2
    Everything is working except it doesnt say You have successfully Registered and it doesnt put it into the database.