Search Unity

Login in a database

Discussion in 'Scripting' started by ericspataru, Nov 23, 2017.

  1. ericspataru

    ericspataru

    Joined:
    Jan 31, 2017
    Posts:
    54
    Firstly, I tried to make the problem sound as easy as possible, I'd like to say a big thank you for everyone who takes time to read this.

    My aim


    My aim is to be able to tell if a login was successful, and then to identify the player`s name and eventually to be able to modify data from my database from the unity script for that specific player. Basically to learn how the WWW form functions.

    What do I have at this moment?

    I have a website set up (I think I`m not allowed to type it here)
    On the website, you can login and register. The user is added to the database with all of the data that I want to have.
    In the game, I made a login system, which upon entering the data and pressing the "Login" button, it tells whether your login credentials were correct or if for example the password/username was incorrect. I made it somehow to print in the console whether the login was successful or not, but I do not know how to access "live" print logs in my script. I suppose there is another method to tell whether the user successfully logged in or not.

    The scripts

    oldLogin.php

    Code (CSharp):
    1. <?php
    2.  
    3. $servername = "I swear to God this is not the problem";
    4. $server_username = "I swear to God this is not the problem";
    5. $server_password = "I swear to God this is not the problem";
    6. $DBName = "I swear to God this is not the problem";
    7.  
    8. $user_username = $_POST["usernamePost"];
    9. $user_password = $_POST["passwordPost"];
    10.  
    11. $conn = new mysqli ($servername, $server_username, $server_password, $DBName);
    12.  
    13. if (!$conn){
    14.     die ("Connection Failed.".mysqli_connect_error());
    15. }
    16.  
    17. $sql = "SELECT password FROM users WHERE username = '".$user_username."' ";
    18. $result = mysqli_query ($conn, $sql);
    19.  
    20. if (mysqli_num_rows ($result) > 0){
    21.     while ($row = mysqli_fetch_assoc($result)){
    22.         if ($row['password'] == $user_password){
    23.             echo "login success";  
    24.         }
    25.         else{
    26.             echo "password incorrect";
    27.         }
    28.     }
    29. }
    30.  
    31. else{
    32.     echo "user not found";
    33. }
    34. ?>
    login.cs

    Code (CSharp):
    1. public InputField userNameIF; // the input field for username
    2.     public InputField passwordIF; // for password
    3.  
    4.     string LoginURL = "http://IdoNotThinkIAmAllowedToShowThis.com/oldLogin.php";
    5.  
    6.     public void submitLogin(){
    7.         StartCoroutine (LogInToDB (userNameIF.text, passwordIF.text)); // I call this function upon clicking the Login button
    8.     }
    9.  
    10.      
    11.     IEnumerator LogInToDB(string username, string password){
    12.         WWWForm form = new WWWForm ();
    13.         form.AddField ("usernamePost", username);
    14.         form.AddField ("passwordPost", password);
    15.  
    16.         WWW www = new WWW (LoginURL, form);
    17.         yield return www;
    18.  
    19.         print (www.text);
    20.     }
     
  2. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
  3. ericspataru

    ericspataru

    Joined:
    Jan 31, 2017
    Posts:
    54
    Hi and thank you very much for your interest!

    The purpose of my project is mainly to help me learn. The link you offered me contains a fairly complicated system which I don't understand. I would really love a solution to my problem, rather than an already scripted system. But still, thank you very much!
     
  4. ericspataru

    ericspataru

    Joined:
    Jan 31, 2017
    Posts:
    54
    I solved my problem using the php script.
    Instead of echoing "login success", I echoed the $_POST["usernamePost"].
    For the else cases, I returned: "failed:(nameOfTheError)"
    In the c# script, I verified if the sixth characters equals ':', and if not, then the user is logged in with the $_POST["usernamePost"]. I convert www.text into a string, and that is my username.