Search Unity

Problem with Login/Register system in PHP, Please help!

Discussion in 'Scripting' started by DrSega, Jun 26, 2012.

  1. DrSega

    DrSega

    Joined:
    Mar 6, 2012
    Posts:
    157
    Hey guys i am tryign to make a login/register system and here is my database script made in C# in Unity:

    Database:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class database : MonoBehaviour
    6. {
    7.     public static string user = "";
    8.     private string password = "" , rePw = "" , message = "";
    9.    
    10.     private bool register =  false;
    11.    
    12.     private void OnGUI()
    13.     {
    14.         if(message != "")
    15.             GUILayout.Box(message);
    16.        
    17.         if(register)
    18.         {
    19.         GUILayout.Label("Username");
    20.         user = GUILayout.TextField(user);
    21.         GUILayout.Label("Password");
    22.         password = GUILayout.PasswordField(password, "*"[0]);
    23.         GUILayout.Label("Confirm Password");
    24.         rePw = GUILayout.PasswordField(rePw , "*"[0]);
    25.            
    26.             GUILayout.BeginHorizontal();
    27.            
    28.             if(GUILayout.Button("Back"))
    29.                 register = false;
    30.        
    31.         if(GUILayout.Button("Register"))
    32.         {
    33.             message = "";
    34.            
    35.             if(user == "" || password == "")
    36.                 message += "Please enter all the fields \n";
    37.             else
    38.             {
    39.                 if(password == rePw)
    40.                 {
    41.                     WWWForm form = new WWWForm();
    42.                     form.AddField("user" , user);
    43.                     form.AddField("password" , password);
    44.                     WWW w = new WWW("http://f7-preview.awardspace.com/leagueofdeath.com/Register.php", form);
    45.                     StartCoroutine(registerFunc(w));
    46.                 }
    47.             else
    48.                 message += "Your Passwords do not match \n";
    49.             }
    50.         }
    51.             GUILayout.EndHorizontal();
    52.     }
    53.         else
    54.         {
    55.             GUILayout.Label("Username");
    56.             user = GUILayout.TextField(user);
    57.             GUILayout.Label("Password");
    58.             password = GUILayout.PasswordField(password, "*"[0]);
    59.            
    60.             GUILayout.BeginHorizontal();
    61.            
    62.             if(GUILayout.Button("Login"))
    63.             {
    64.                 message = "";
    65.                
    66.                 if(user == "" || password == "")
    67.                     message += "Please enter all the fields \n";
    68.                 else
    69.                 {
    70.                     WWWForm form = new WWWForm();
    71.                     form.AddField("user" , user);
    72.                     form.AddField("password" , password);
    73.                     WWW w = new WWW("http://f7-preview.awardspace.com/leagueofdeath.com/Login.php", form);
    74.                     StartCoroutine(login(w));
    75.                 }
    76.             }
    77.            
    78.             if(GUILayout.Button("Register"))
    79.             {
    80.                 register = true;
    81.             }
    82.            
    83.             GUILayout.EndHorizontal();
    84.         }
    85.     }
    86.    
    87.     IEnumerator login(WWW w)
    88.     {
    89.         yield return w;
    90.         if(w.error == null)
    91.         {
    92.             if(w.text == "Success")
    93.             {
    94.                 print("Like a bos?");
    95.             }
    96.             else
    97.             message += w.text;
    98.         }
    99.         else
    100.         {
    101.             message += "ERROR: " + w.error + "\n";
    102.         }
    103.     }
    104.    
    105.     IEnumerator registerFunc(WWW w)
    106.     {
    107.         yield return w;
    108.         if(w.error == null)
    109.         {
    110.             message += w.text;
    111.         }
    112.         else
    113.         {
    114.             message += "ERROR: " + w.error + "\n";
    115.         }
    116.     }
    117. }
    118.  
    Register PHP code:
    Code (csharp):
    1.  
    2. <?PHP
    3.  
    4. $user = $_POST['user'];
    5. $pass = $_POST['password'];
    6.  
    7.  
    8. $con = mysql_connect("fdb2.awardspace.com","Username","password") or ("Cannot Connect!" . mysql_error());
    9. if (!$con)
    10.     die('Could not Connect: ' . mysql_error());
    11.    
    12. mysql_select_db("username" , $con) or die ("could not load the database" . mysql_error());
    13.  
    14. $check = mysql_query("SELECT * FROM LoD WHERE `user`='".$user."'");
    15. $numrows = mysql_num_rows($check);
    16. if($numrows == 0)
    17. {
    18.     $pass = md5($pass);
    19.     $ins = mysql_query("INSERT INTO `LoD` ( `id` , `user` , `pass` ) VALUES ( '' , '.$user.' , '.$pass.' ) ; ");
    20.     if($ins)
    21.         die ("Successfully created account!");
    22.     else
    23.         die ("Error: " . mysql_error());
    24. }
    25. else
    26. {
    27.     die("User already exists!");
    28. }
    29.  
    30.  
    31. ?>
    32.  
    Login PHP code:
    Code (csharp):
    1.  
    2. <?PHP
    3.  
    4. $user = $_POST['user'];
    5. $pass = $_POST['password'];
    6.  
    7.  
    8. $con = mysql_connect("fdb2.awardspace.com","Username","password") or ("Cannot Connect!" . mysql_error());
    9. if (!$con)
    10.     die('Could not Connect: ' . mysql_error());
    11.    
    12. mysql_select_db("username" , $con) or die ("could not load the database" . mysql_error());
    13.  
    14. $check = mysql_query("SELECT * FROM LoD WHERE `user`='".$user."'");
    15. $numrows = mysql_num_rows($check);
    16. if($numrows == 0)
    17. {
    18.     die("Username does not exist \n");
    19. }
    20. else
    21. {
    22.     $pass = md5($pass);
    23.     while($row = mysql_fetch_assoc($check))
    24.     {
    25.         if ($pass = $row['pass'])
    26.             die("Success");
    27.         else
    28.             die("Password does not match \n");
    29.     }
    30. }
    31.  
    32. ?>
    33.  
    What is wrong in my code that is making it do this?

    Please help? :p
     
    Last edited: Jun 27, 2012
  2. DrSega

    DrSega

    Joined:
    Mar 6, 2012
    Posts:
    157
    bump?
     
  3. kilt

    kilt

    Joined:
    Oct 12, 2011
    Posts:
    136
    are you really telling us your SQL info?

    anyways from what I can see

    in register you are inserting

    '.$user.'

    in log in you are searching for

    '".$user."'


    you gotta give more info tbh

    edit: and i could be totally wrong

    edit1: ya and im totally wrong i think
     
    Last edited: Jun 27, 2012
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,450
    well.. does it give any error?
    or whats the actual problem?
    does it create account in db or not, it cannot login or ...?
     
  5. kilt

    kilt

    Joined:
    Oct 12, 2011
    Posts:
    136
    if ($pass = $row['pass'])

    should be

    if ($pass == $row['pass'])
     
  6. DrSega

    DrSega

    Joined:
    Mar 6, 2012
    Posts:
    157
    Right :D
     
    Last edited: Feb 16, 2013
  7. DrSega

    DrSega

    Joined:
    Mar 6, 2012
    Posts:
    157
    I can register fine, and it adds the account to the database, but when i try login i type the username correct as well as the password but it continues to say "Username does not exist".
     
  8. DrSega

    DrSega

    Joined:
    Mar 6, 2012
    Posts:
    157
    can any1 help me? pls??????
     
  9. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Not sure how much help you're going to get here. This isn't a PHP/MySQL forum.
     
  10. DrSega

    DrSega

    Joined:
    Mar 6, 2012
    Posts:
    157
    But im using it for Unity, and im sure lots of people have dont a register/login system for their games.
     
  11. DrSega

    DrSega

    Joined:
    Mar 6, 2012
    Posts:
    157
    i found out that my register PHP script adds it to my database as

    .username. (username as of the one u registered with, and i found out i have to include the .'s to log in,

    Any ideas how to make it so that i dont have to add the .'s?
     
    Last edited: Feb 16, 2013
  12. roka

    roka

    Joined:
    Sep 12, 2010
    Posts:
    598
    $check = mysql_query('SELECT pass FROM LoD WHERE user="'.$user.'"');
    $dn = mysql_fetch_array($check);

    if($dn['pass']==md5($pass) and mysql_num_rows($check)>0){
    echo("Success");
    mysql_close();
    exit();

    }else if($dn['pass']!=md5($pass) and mysql_num_rows($check)>0){

    echo "Invalid Password";
    mysql_close();
    exit();

    }else if(mysql_num_rows($check)==0){

    echo " Account doesn't exist ,create account";

    }
     
  13. DrSega

    DrSega

    Joined:
    Mar 6, 2012
    Posts:
    157
    Will try
     
    Last edited: Feb 16, 2013
  14. roka

    roka

    Joined:
    Sep 12, 2010
    Posts:
    598
    if you don't know where you need to add this , i can't really help you more .....
    Understand what you do my friend
     
  15. DrSega

    DrSega

    Joined:
    Mar 6, 2012
    Posts:
    157
    i Just dont want to mess my script up by mistake, but ill try :) thank you thought xD
     
  16. roka

    roka

    Joined:
    Sep 12, 2010
    Posts:
    598
    make a backup .....
     
  17. DrSega

    DrSega

    Joined:
    Mar 6, 2012
    Posts:
    157
    i did, testing it now, but from what i see in the database something might be wrong with my register script as it saves the username name and password that the person typed with dots at the beginning and end. I always have to enter my username as .DrSega. for it to recognize it but if i type my password correctly with or without the dots it still says it is wrong?
     
  18. DrSega

    DrSega

    Joined:
    Mar 6, 2012
    Posts:
    157
    ok ty, i fixed all that ty so much Roka!
     
    Last edited: Jun 27, 2012
  19. DrSega

    DrSega

    Joined:
    Mar 6, 2012
    Posts:
    157
    Thanks everybody :)
     
  20. AbdiRT

    AbdiRT

    Joined:
    Oct 6, 2016
    Posts:
    2
    rokaaaaa