Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

MMORPG style login and registration system?

Discussion in 'Multiplayer' started by GameCode4878, Feb 9, 2016.

  1. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    I have taken classes on how to work with code, but I still need help with this game project. I know how to make the buttons and text fields for the Login GUI and Account registration GUI,but the problem is: I don't know how to make them functional. I have heard something about using a server side database, but I have almost no idea how to use them. Can someone help me figure out how to create a functional login and account creation GUI that communicates with a server side database? If I can figure that out, then more than half the problems I face in creating this game would go away. I also have heard about PHP but am not sure how to use it. Basicly here is what I need help with:
    1) creating a server side database to store player data (such as username and password),
    2) creating a Login and account creation GUI that is functional with the database and
    3) (may be a later function but is not a priorty as of now) create a way for the database to also store the player's stats, such as how much in-game money he has and his combat level.

    also the database ABSOLUTELY CAN'T be client side, as if it were, it could be tampered with (or in other words, hacked) much more easily.

    Can anyone help me accomplish creating this idea? And by the way, I am working in C#.
     
    Last edited: Feb 9, 2016
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,860
    OK, I wasn't going to reply because I don't have much helpful to say, but I can't stand to see you get NO reply, so here we go.

    There's a good possibility that what you're attempting to do is beyond your current capabilities. You certainly WILL have to use PHP, or some other server-side programming solution (any of which are going to be at least as hard as PHP, probably harder). If you barely know what PHP is, then you have a few months of learning ahead of you, at the very least, getting up to speed on that. And yes, from your description, the PHP will need to speak to some server-side database, so you also have to learn how to set up and manage a database (MySQL is common, but there are many other choices, including cloud services these days).

    Once you know how to make something like a db-backed REST service in PHP, then communicating with it in Unity is comparably easy, thanks to the WWW and WWWForm classes. But here too, you will probably need more than a beginner's level of Unity proficiency to see how to make the GUI, grab the inputs and build the request, handle the response from the server when it eventually comes back, etc.
     
  3. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    I agree with @JoeStrout . I have already done this (written both the C# code needed for Unity to communicate with the database, and the PHP code to handle Unity's request and supply it with responses). I could just give you the code (and if you want me to I will). However, as @JoeStrout said, you have a LOT of learning ahead of you. Registering and logging in isn't half the battle, hell it's not even 10% of it. You have to handle saving player data to the database, and then requesting that data when the player successfully logs in. What if the player wants to make changes to their registration data? You'll have to know how to push those changes to the database.

    My suggestion: As @JoeStrout said, go learn PHP. Also learn MySQL (it's not a coding language, it's an interface). Learn more about the C# (.NET) library. Then try and tackle this.
     
  4. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    Can you show me the script? maybe I can learn something form it. Then how will I make a database to connect to it? Thanks.
     
  5. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    I use Award Space as a server to hold my database.

    The following two scripts are the PHP scrips you need. The first is for logging in. The second is for registration.
    <?PHP

    $username = $_POST['username'];
    $password = $_POST['password'];

    $con = mysql_connect("fdb13.awardspace.net","INSERT YOUR MYSQL USERNAME HERE","INSERT YOUR MYSQL PASSWORD HERE") or ("Cannot connect!" . mysql_error());
    if (!$con)
    die('Could not connect: ' . mysql_error());

    mysql_select_db("1870311_admin" , $con) or die ("could not load the database" . mysql_error());

    $check = mysql_query("SELECT * FROM accounts WHERE `username`='".$username."'");
    $numrows = mysql_num_rows($check);
    if ($numrows == 0)
    {
    die ("Username does not exist.");
    }
    else
    {
    $password = md5($password);
    while($row = mysql_fetch_assoc($check))
    {
    if ($password == $row['password'])
    die("Log in successful!");
    else
    die("Incorrect password.");
    }
    }

    ?>
    <?PHP

    $name = $_POST['name'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    $email = $_POST['email'];

    $con = mysql_connect("fdb13.awardspace.net","INSERT YOUR MYSQL USERNAME HERE","ENTER YOUR MYSQL PASSWORD HERE") or ("Cannot connect!" . mysql_error());
    if (!$con)
    die('Could not connect: ' . mysql_error());

    mysql_select_db("1870311_admin" , $con) or die ("could not load the database" . mysql_error());

    $check = mysql_query("SELECT * FROM accounts WHERE `username`='".$username."'");
    $numrows = mysql_num_rows($check);
    if ($numrows == 0)
    {
    $password = md5($password);
    $ins = mysql_query("INSERT INTO `accounts` (`id` , `name` , `username` , `password` , `email`) VALUES ('' , '".$name."' , '".$username."' , '".$password."' , '".$email."') ; ");
    if ($ins)
    die ("Succesfully Created User!");
    else
    die ("Error: " . mysql_error());
    }
    else
    {
    die("User already exists!");
    }


    ?>

    The following script is used in Unity to submit requests to the server to register and/or log in.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. /*
    5. THE FOLLOWING SCRIPT WAS WRITTEN BY DAVID ROSEN. IT IS FREE TO USE WITH ACKNOWLEDGEMENT OF THE CREATOR.
    6. IT IS INTENDED FOR USE WITH AWARD SPACE (https://www.awardspace.net/), PHP and MySQL
    7. */
    8. public class Main_Menu : MonoBehaviour
    9. {
    10.     //THE FOLLOWING ARE REFERENCES TO THE UI INPUT FIELDS THAT THE PLAYER TYPES INTO
    11.     public Text usernameText, passwordText, regNameText, regUsernameText, regPassText, regConfPassText, regEmailText;
    12.     public Text messageText; //THIS IS THE TEXT TO BE DISPLAYED ON SCREEN TO THE PLAYER
    13.    
    14.     //THE FOLLOWING VARIBALES ARE POPULATED BY THE TEXT THE PLAYER ENTERS INTO THE CORRESPONDING INPUT FIELDS
    15.     private string username, password, regName, regUsername, regPass, regConfPass, regEmail;
    16.    
    17.     //FUNCTION TO BE CALLED VIA THE UI BUTTON
    18.     public void LogIn()
    19.     {
    20.         messageText.text = ""; //CLEAR ANY DISPLAYED MESSAGES TO THE PLAYER
    21.        
    22.         username = usernameText.text;    //POPULATE THE PRIVATE username VARIABLE WITH THE TEXT THE PLAYER ENTERED INTO THE usernameText INPUT FIELD
    23.         password = passwordText.text;    //POPULATE THE PRIVATE password VARIABLE WITH THE TEXT THE PLAYER ENTERED INTO THE passwordText INPUT FIELD
    24.        
    25.         if (username == "" || password == "") //IF THE PLAYER HASN'T ENTERED THE REQUIRED INFORMATION...TELL THEM TO
    26.             messageText.text = "Please complete all fields.";
    27.         else    //IF ALL INFORMATION IS ENTERED, BUILD A WWWForm AND SEND IT TO THE SERVER
    28.         {
    29.             WWWForm form = new WWWForm();
    30.             form.AddField("username", username);
    31.             form.AddField("password", password);
    32.             WWW w = new WWW("http://???????????????.dx.am/login.php", form);    //REPLACE ?????????? WITH YOUR AWARD SPACE DOMAIN
    33.             StartCoroutine(LogIn(w));
    34.         }
    35.     }
    36.     //FUNCTION TO BE CALLED VIA THE UI BUTTON
    37.     public void Register()
    38.     {
    39.         messageText.text = ""; //CLEAR ANY DISPLAYED MESSAGES TO THE PLAYER
    40.        
    41.         regName = regNameText.text;    //POPULATE THE PRIVATE regName VARIABLE WITH THE TEXT THE PLAYER ENTERED INTO THE regNameText INPUT FIELD
    42.         regUsername = regUsernameText.text;    //POPULATE THE PRIVATE regUsername VARIABLE WITH THE TEXT THE PLAYER ENTERED INTO THE regUsernameText INPUT FIELD
    43.         regPass = regPassText.text;    //POPULATE THE PRIVATE USERNAME regPass WITH THE TEXT THE PLAYER ENTERED INTO THE pregPassText INPUT FIELD
    44.         regConfPass = regConfPassText.text;    //POPULATE THE PRIVATE regConfPass VARIABLE WITH THE TEXT THE PLAYER ENTERED INTO THE regConfPassText INPUT FIELD
    45.         regEmail = regEmailText.text;    //POPULATE THE PRIVATE regEmail VARIABLE WITH THE TEXT THE PLAYER ENTERED INTO THE regEmailText INPUT FIELD
    46.        
    47.         if (regName == "" || regUsername == "" || regPass == "" || regConfPass == "" || regEmail == "") //IF THE PLAYER HASN'T ENTERED THE REQUIRED INFORMATION...TELL THEM TO
    48.             messageText.text = "Please complete all fields.";
    49.         else    //IF ALL INFORMATION IS ENTERED......
    50.         {
    51.             if(regPass == regConfPass)    //......AND THE PASSWORDS MATCH, BUILD A WWWForm AND SEND IT TO THE SERVER
    52.             {
    53.                 WWWForm form = new WWWForm();
    54.                 form.AddField("name", regName);
    55.                 form.AddField("username", regUsername);
    56.                 form.AddField("password", regPass);
    57.                 form.AddField("email", regEmail);
    58.                 WWW w = new WWW("http://???????????.dx.am/register.php", form);    //REPLACE ?????????? WITH YOUR AWARD SPACE DOMAIN
    59.                 StartCoroutine(Register(w));
    60.             }
    61.             else    //......AND THE PASSWORDS DON'T MATCH, TELL THE PLAYER
    62.                 messageText.text = "Your passwords do not match.";
    63.         }
    64.     }
    65.    
    66.     //WE USE COROUTINES TO SEND INFORMATION TO THE SERVER, SO THAT WE CAN WAIT FOR A RESPONSE
    67.     private IEnumerator LogIn(WWW _w)
    68.     {
    69.         yield return _w;    //WAIT FOR A RESPONSE FROM THE SERVER
    70.  
    71.         if (_w.error == null)    //IF THE SERVER DOESN'T SEND BACK AN ERROR
    72.         {
    73.             if (_w.text == "Log in successful!")    //THE PHP SCRIPT SUPPLIED WILL SEND THIS MESSAGE BACK IF THE LOGIN WAS SUCCESSFUL
    74.             {
    75.                 // WHAT HAPPENS WHEN THE PLAYER LOGS IN
    76.             }
    77.             else
    78.                 messageText.text = _w.text;    //THE PHP SCRIPT SUPPLIED WILL TELL THE PLAYER IF THEIR PASSWORD IS INCORRECT, OR IF THEIR USERNAME DOESN'T EXIST
    79.         }
    80.         else
    81.             messageText.text = "ERROR: " + _w.error;    //IF THERE IS AN ERROR (SUCH AS THE SERVER BEING DOWN) THE PHP SCRIPT SUPPLIED WILL TELL THE PLAYER
    82.     }
    83.     private IEnumerator Register(WWW _w)
    84.     {
    85.         yield return _w;    //WAIT FOR A RESPONSE FROM THE SERVER
    86.  
    87.         if (_w.error == null)    //IF THE SERVER DOESN"T SEND BACK AN ERROR
    88.             messageText.text = _w.text;        //THE PHP SCRIPT SUPPLIED WILL SEND A MESSAGE BACK TO THE PLAYER SAYING REGISTRATION WAS COMPLETED
    89.         else
    90.             messageText.text = "ERROR: " + _w.error;    //IF THERE IS AN ERROR (SUCH AS THE SERVER BEING DOWN) THE PHP SCRIPT SUPPLIED WILL TELL THE PLAYER
    91.     }
    92. }
    You're going to have to figure out how to do the rest on your own buddy. I'd be handicapping you if I gave you anymore.
     
  6. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    thanks, hope this works.
     
  7. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Game_Menu_GUI : MonoBehaviour {
    6.    
    7.     public string Username = "";
    8.     public string Password = "";
    9.    
    10.     private string CreateUsername = "";
    11.     private string CreatePassword = "";
    12.     private string ConfirmPassword = "";
    13.     private string PlayerEmail = "";
    14.    
    15.     public string CurrentGameMenu = "Login";
    16.    
    17.     public string MenuText = "";
    18.     public string SecondMenuText = "";
    19.    
    20.     void OnGUI () {
    21.         if(CurrentGameMenu == "Login"){
    22.             Login();
    23.         }else if (CurrentGameMenu == "CreateAccount") {
    24.             CreateAccount():
    25.         }
    26.     }
    27.     void Login () {
    28.         GUI.Label(new Rect(200, 250, 200, 25), "Enter your username:");
    29.         Username = GUI.TextField(new Rect(200, 275, 200, 25), Username);
    30.        
    31.         GUI.Label(new Rect(200, 300, 200, 25), "Enter your password:");
    32.         Password = GUI.TextField(new Rect(200, 325, 200, 25), Password);
    33.        
    34.         GUI.Label(new Rect(200, 350, 200, 25), MenuText);
    35.        
    36.         if(GUI.Button(new Rect(200, 375, 200, 25), "Login")){
    37.             if(Username == "" || Password == ""){
    38.                 MenuText == "Please fill in all info";
    39.             }else {
    40.                 WWWForm form = new WWWForm();
    41.                 form.AddField("Username", Username);
    42.                 form.AddField("Password", Password);
    43.                 WWW w = new WWW("http://???????????????.dx.am/login.php", form);
    44.                 StartCorountine(LogIn(w));
    45.             }
    46.         }
    47.         if(GUI.Button(new Rect(200, 400, 200, 25), "Create an account")){
    48.             CurrentGameMenu = "CreateAccount";
    49.         }
    50.     }
    51.     void CreateAccount () {
    52.         GUI.Label(new Rect(200, 250, 200, 25), "Enter a username:");
    53.         CreateUsername = GUI.TextField(new Rect(200, 275, 200, 25), CreateUsername);
    54.        
    55.         GUI.Label(new Rect(200, 300, 200, 25), "Enter a password:");
    56.         CreatePassword = GUI.TextField(new Rect(200, 325, 200, 25), CreatePassword);
    57.        
    58.         GUI.Label(new Rect(200, 350, 200, 25), "Confirm password:");
    59.         ConfirmPassword = GUI.TextField(new Rect(200, 375, 200, 25), ConfirmPassword);
    60.        
    61.         GUI.Label(new Rect(200, 400, 200, 25), "Enter your email:");
    62.         PlayerEmail = GUI.TextField(new Rect(200, 425, 200, 25), PlayerEmail);
    63.        
    64.         GUI.Label(new Rect(200, 450, 200, 25), SecondMenuText);
    65.        
    66.         if(GUI.Button(new Rect(200, 475, 200, 25), "Create Account")){
    67.             if(CreateUsername != "" || PlayerEmail != ""){
    68.                 if(CreatePassword == ConfrirmPassword){
    69.                     WWWForm form = new WWWForm();
    70.                     form.AddField("CreateUsername", CreateUsername);
    71.                     form.AddField("ConfirmPassword", ConfirmPassword);
    72.                     WWW w = new WWW("http://???????????.dx.am/register.php", form);
    73.                     StartCorountine(CreateAccount(w));
    74.                 }
    75.             }  
    76.         }
    77.         if(GUI.Button(new Rect(200, 500, 200, 25), "Cancel Account Creation")){
    78.             CurrentGameMenu = "Login";
    79.         }
    80.     }
    81.     private IEnumerator Login(WWW _w) {
    82.         yield return _w;
    83.         if(_w.error == null){
    84.             if(_w.text == "Log in successful!"){
    85.                 //What happens to the player when he logs in:
    86.             }else {
    87.                 MenuText = _w.text;
    88.             }
    89.         }else {
    90.             MenuText = "Error" + _w.error;
    91.         }
    92.     }
    93.     private IEnumerator CreateAccount(WWW _w) {
    94.         yield return _w;
    95.         if(_w.error == null) {
    96.             SecondMenuText = _w.text;
    97.         }else {
    98.             SecondMenuText = "Error" + _w.error;
    99.         }
    100.     }
    101. }

    I have taken the code you gave me and changed a few things for my game's purpose. Will something like this code work?
     
  8. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    where do I put the phps? in the client? if not where? does the domain have to have web hosting?
     
  9. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    where and how do i create the php files?
     
    Last edited: Feb 11, 2016
  10. KingfisherWyvernStudio

    KingfisherWyvernStudio

    Joined:
    Oct 5, 2011
    Posts:
    324
    If you have questions like this, I think it's best if you start learning PHP asap. Start here:
    PHP
    MySQL

    Yes, if you want to work with server-side and client-side, you need to have webhosting. A webhoster which offers PHP and MySQL at least.
    Like other's have already said, you can use WWW and WWWForm classes which you can check in Unity's User manual, but I strongly recommend learning about PHP and MySQL first! And additionally some basic HTML as well so that you know how to build up the forms and such. If you don't have any knowledge, at least basic knowledge, about these programming languages, it will be very hard to understand what's needed in Unity to achieve what you want.

    Good luck.
     
  11. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    ok, I have set up the php's but do I have to create a table in the database? Or will the scripts do that automatically?
     
  12. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    You're asking for somebody to take your hand and walk you through everything. No one here is going to do that. There are WAY too many tutorials out there if you just look. Start here ----->
     
    k0fe and KingfisherWyvernStudio like this.
  13. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    The code I made has worked for a little, but as I contiued adding to It I got error CS0103,
    start corountine does not exist in its current context. I have noticed something in the video, could this error be caused be the IEnumerators being private?
     
    Last edited: Feb 12, 2016
  14. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    When I create the database it will not give the rows "Extra" and other parts to the structure as shown in your video, I think its outdated. How do I edit the types of rows i get in the database?
     
  15. KingfisherWyvernStudio

    KingfisherWyvernStudio

    Joined:
    Oct 5, 2011
    Posts:
    324
    Please, like DRRosen3 already suggested, search for some tutorials on PHP and MySQL first! START with PHP and MySQL, like the ones I posted earlier. These will teach you how to set up databases, work with PHP to fill your databases and more things like that. A quick search for more tuturials about PHP and SQL on Google gave me this result. It really is best if you start with the two links I already gave (SQL and PHP). That gives you a BASIC UNDERSTANDING of both PHP and MySQL, how to work with database, how to fill them with data, how to read the data, etc. From there you can move forward with, for example a Register and login concept just in a website. Once you understand how that works, you can move on to learn how to do this in Unity.

    Seen your, seemingly, lack of enough knowledge, I'd suggest to find a (free) webhost which offers hosting options with both PHP and MySQL to try out the tutorials before you move on to anything else.

    I cannot stress this enough when you want to make a game/app that requires a login system (or any other system for which you need a database outside of your game client): LEARN THE BASICS of communicating with a database BEFORE you start building the game in unity. Learn to make your own registration and login system BEFORE you start building the game in unity. Do NOT simply copy & paste without understanding anything! Otherwise you'll end up with bucketloads full of questions about basics that have nothing to do with Unity before you can really start in Unity.

    Start with something very simple like making PHP on a webpage say "Hello world" and go from there. I know it takes some time to learn it all (I have done it while I was in a completely different field of work. I had to learn it all by myself, without any teachers at school or any collegues at work who knew what they were talking about), but it really is the best way. Otherwise you will not understand what to do if you get error message. You wouldn't have a clue where to start looking for an answer, like you don't now. In essence, you would have a game, but you wouldn't have the skills to properly maintain the game and its userbase and offer a good support when people start to play your game and run into problems (register, login, things that need to be fixed in the database, etc).

    I hope this was helpful for you.
     
    DRRosen3 likes this.
  16. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    The problem currently is not lack of knowledge, but the fact that the tutorial is outdated, and the interface for editing the databases has changed in award space. The database editor looks totally different from the one in video. If I only knew how to use the new editor, I would probably be finished by now. I have successfully set up the phps, and the web host, and the script, but can't figure out how to use the database editor to work with the codes and phps.
    basically the database editor in award space looks different now than when it did in the video. But once the database is complete, the login screen should become operational.
     
    Last edited: Feb 12, 2016
  17. KingfisherWyvernStudio

    KingfisherWyvernStudio

    Joined:
    Oct 5, 2011
    Posts:
    324
    Okay..... Well, I don't have my site hosted on Award Space. Which database system do they use there? PhP My Admin?
     
  18. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
  19. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    What did you use to create the site and other stuff?
     
  20. KingfisherWyvernStudio

    KingfisherWyvernStudio

    Joined:
    Oct 5, 2011
    Posts:
    324
    My portfolio site (which works with databases) is on NameCheap which is a paid hosting account.
    I use PHP MyAdmin as well, but my host has a seperate Creation system for databases. I simply give the name of the database I want to create and the system automatically sets it up.

    I use Mysql to create the tables in my database which means I run a a SQL query on the database to create the table.
     
  21. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    but I am using php my adimin but it will not let me add fields or make user ids able to auto increment, How can I modify the database to do this?
     
  22. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    I have noticed I can create a table another way by entering a php code, but I don't know how to create one for the table used in the tutorial. Through the previous help I have recieved I now have some understanding on how the client communicates to the server and database. But I need a code to create the database table like the one in the tutorial. I am not a total NOOB to code as I have taken classes on it. But the classes I have taken are on C#, not php or mysql. I have a webhost, I have the script set up, the database is set up, but I can't create the table to store the player's info. I can create a table, but it does not show me how to make it look like the one in the tutorial video.

    I have create a table, but how do I give it the abillity to auto increment and register the player's id, username, password, and email as "fields" instead of names? As I have said, the database editor has had updates, and I have little idea how to make "accounts" table because of the updates.
     
    Last edited: Feb 12, 2016
  23. KingfisherWyvernStudio

    KingfisherWyvernStudio

    Joined:
    Oct 5, 2011
    Posts:
    324
    This is one of the reasons why I suggested you start learning PHP with MySQL so that you can run sql queries on the database. You can do this in PHP MyAdmin as well (the SQL tab lets you do that). Here's an excellent example of how you can create a table with the sql queries. It will help you better understand how the PHP and SQL queries work together.
     
  24. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    soon I will test the codes and stuff to see if they work. Thanks everyone.
     
    Last edited: Feb 12, 2016
  25. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    Honestly, the tutorial isn't outdated. It was created during the phpAdmin 2 era. Presently we're in the phpAdmin 4 era, but nothing has really changed. You'd know that if you did as @Sheriziya keep suggesting, and LEARN about the languages and programs you're trying to use, before you use them. A MySQL query is the same in phpAdmin 4 as it was in phpAdmin 2. The only thing that has really changed in all the years is the syntax and the fact that everything went from mysql to mysqli...but even the tutorial (IIRC) already used mysqli.

    If you choose not to take our advice that's on you. But I for one won't be helping you when you make a new post asking for help, which has a subject that you wouldn't need help with, if you took our advice in the first place.
     
  26. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    I have just finnished creating the database, When I get home to my primary computer, I will go see if it works (This is my backup computer I'm on now, my main one with unity on it is at home). Thanks for all the help. Will be back shortly if something goes wrong
     
  27. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    I get an error 403 forbidden, what now? it also says mysql_connect() is depreciated.
     
  28. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    :rolleyes: This is EXACTLY what me and @Sheriziya were talking about.
     
  29. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    ok, I still face the 403 error, it will not let me access the websites for the phps. Also the client faces similar issues.
     
  30. FStar

    FStar

    Joined:
    Sep 11, 2015
    Posts:
    50
    Why do you use PHP for database access? There are .NET libraries for accessing PostgreeSQL and some other databases that can be used with Unity personal..
     
  31. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    I have to upgrade my award space account to use that, I need php. Question, in the php scripts, can I just change the "mysql" to "mysqli"? If so, can a MySQL database work with that?
     
    Last edited: Feb 15, 2016
  32. KingfisherWyvernStudio

    KingfisherWyvernStudio

    Joined:
    Oct 5, 2011
    Posts:
    324
    Please, search Google for your answer. I'm sorry, but like DRRosen3 and I have repeatedly advised you already, LEARN about the languages on a basic level BEFORE you start using them in conjuction with Unity. I'm sorry, I can't dig deeper into this without teaching you everything I know about PHP and MySQL, which BTW, I learned all by myself. At this point there's nothing else I can advise and tell you to do now besides:
    LEARN THE BASICS!!!!!

    I'm sorry, I don't want to be harsh, but we warned you multiple times about runnign into things like this if you don't take the time to properly learn the languages. If you don't want to take the time to properly learn languages you want to work with, I can't and won't help you further. :rolleyes:
     
  33. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    every time I try to look up an answer on google, I never find the right answer or I find something that only leads me to another question, this is why I go to people for help instead of google. If I went to google every time I needed an answer, I will never get one, or I will get the wrong one. I have not found even one successful tutorial on how to do this, neither have I found out of any websites that can teach how stuff like this works. Every time I try to find a tutorial, I can spend hours at a time looking and reading, trying to learn how things work only to come up empty handed. If I tried it the way you wanted me to, It would take me years to accomplish, and by then I probably would have something better to do. Do you understand my problem? I have already tried at least a hundred times (and yes I have kept count) to research what I needed to know, only to have found out nothing each and every time. Whatever I have found has proven to be unhelpful. The things posted in this forum have got me so close to the answer, and unless something happens soon, I am afraid I will never get this close again.
     
    Last edited: Feb 15, 2016
  34. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    I am not asking to be spoon fed the whole project and just have it given to me. I am simply asking "What am I doing wrong?" I already understand most of how this works. I just need to figure out what am I doing wrong.
     
  35. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    the problem I am currently facing is when I try to type in the url for the phps, it gives me "access denied", or something of that sort.
     
  36. KingfisherWyvernStudio

    KingfisherWyvernStudio

    Joined:
    Oct 5, 2011
    Posts:
    324
    I don't understand why you can't seem the find the answer, when we have already given you several links where you can find the answers to a lot of questions. As to your "mysql_connect() is depreciated" problem, I searched on google and found the answer within less than a minute. Really, the first link that came up gave me the answer........

    The same goes for the "403 forbidden error" and "Access Denied" error....... These are all basic things, you'll learn when you start to learn and understand these languages and website building. Have you ever build a website? When i first started doing that, I ran into those problems you mentioning now. Just plain simple HTML, PHP etc...... No Unity, Unreal or whatever game engine you use. These are basics of WEBSITE BUILDING.

    Yes, I understand you're looking for help, but all the things you've come with, are things we've found very easily on the web, because we started with LEARNING the BASICS first. Only when you know the basics will you understand where to start looking when you run into a problem that might be a bit more complicated.

    Have you done everything from the links I gave you? Do you understand those tutorials? Have you tried to make a webpage (NOT a UNITY app!!!) in which you start using PHP and MySQL? THAT is where you start to learn. Have you tried to find out more information about PHP, like on PHP.net? Have you tried to understand the PHP My Admin better by accessing its documentation, which is linked in PHP MyAdmin? Have you tried to understand MySQL better by going through the documentation that's also linked in PHP MyAdmin?
    START with learning the basics, like we've suggested multiple times. That way you will learn the basics you need before you can start to connect Unity to your database. NEVER, NEVER, NEVER include code when you don't know what it does exactly and don't understand the consequences of it in a game/app/website.

    In short:
    1. INVESTIGATE what it is exactly that your webhost is providing you for PHP (including the version and the settings), MySQL (version), other Database methods, etc.
    2. LEARN to build a simple webpage with HTML (including uploading it to your server, setting the permissions, etc.)
    3. LEARN about error pages, what they mean for the end users (visitors) of your site and what it means for you as a developer of the site.
    4. LEARN and UNDERSTAND PHP and understand how it works with the settings as they are provided by your webhost
    5. LEARN and UNDERSTAND MySQL and understand how it works with the settings as they are provided by your webhost
    6. LEARN to build a simple webpage (NOT in UNITY!!!) with PHP (including uploading it to your server, setting the permissions, etc.)
    7. LEARN to build a simple database on your server by code, instead of the wizard. Only that way you'll actually learn what the settings in the database mean.
    8. LEARN to access and use the database with PHP and MySQL from a webpage, including userrights, etc.
    9. When you have done and understand everything of the above steps, know what happens when you do or don't include certain codes and have a good understanding of how websites react when information is incorrect (like 403, 404, 405 etc pages) only then you can start to translate everything to Unity.
    I know I'm referring you to the very basics and I understand that you'd like to get your game up and running rather than returning back to basics, but all you've mentioned above are things you wouldn't have had to ask if you knew the basics of website building, PHP and MySQL.

    I'm sorry if it seems like I don't want to help you, but seen the error messages you are posting, it's clear to me that you need a lot more basics before you can start building (and supporting!) a game that's connected to a server database instead of a multiplayer game on the system Unity offers.
    And with these basics I mean basics that have absolutely NOTHING to do with Unity, C# or anything else related to Unity.

    I really hope that you'll follow the steps as I described them above. It will make your live a lot easier! And it will give you a lot more fun in developing games and apps that need the external database connections.
     
    DRRosen3 likes this.
  37. LeopardX

    LeopardX

    Joined:
    May 31, 2015
    Posts:
    64
    Ive done this very thing for my game, but i use MSSQL and windows aspx my backend server, basically you will need some kind of vps server or web server that you have full control over, this can eather be windows or linux, linux is cheaper, its more or less down to prefrance at the end of the day.. my exmaples will go down the route of windows server..

    Say you have a login in your game, email and password fields and a button to login, on the login button click you would call something like this..

    using these:
    using UnityEngine;
    using System;
    using System.IO;
    using System.Net;
    using System.Text;
    using System.Net.Security;
    using System.Security.Cryptography.X509Certificates;
    using UnityEngine.Networking;
    using System.Web;

    Code (CSharp):
    1. string post_data = "&email=" + TextFieldEmail.Text + "&pass=" + TextFieldPass.Text;
    2.    
    3.         // this is where we will send it
    4.         string uri = "https://www.yoursite.com/somepage.aspx";
    5.    
    6.         // create a request
    7.         HttpWebRequest request = (HttpWebRequest)
    8.             WebRequest.Create(uri); request.KeepAlive = false;
    9.         request.ProtocolVersion = HttpVersion.Version10;
    10.         request.Method = "POST";
    11.    
    12.         // turn our request string into a byte stream
    13.         byte[] postBytes = Encoding.ASCII.GetBytes(post_data);
    14.    
    15.         // this is important - make sure you specify type this way
    16.         request.ContentType = "application/x-www-form-urlencoded";
    17.         request.ContentLength = postBytes.Length;
    18.    
    19.         // ServerCertificateValidationCallback allows for self signed webserver certificates, otherwise it will fail, remove this and use an offical signed SSL certificate when your being more serious.
    20.         System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
    21.         Stream requestStream = request.GetRequestStream();
    22.    
    23.         // now send it
    24.         requestStream.Write(postBytes, 0, postBytes.Length);
    25.         requestStream.Close();
    26.    
    27.         // grab te response and print it out to the console along with the status code
    28.         HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    29.         string responce = new StreamReader(response.GetResponseStream()).ReadToEnd();
    30.    
    31.         //Deal with the presponce, its what the webserver will send back to eather say login was successful or not.
    Then make an aspx page like this and using..
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Text.RegularExpressions;

    Code (CSharp):
    1.  string email = "";
    2. string pass = "";
    3.  
    4.     protected void Page_Load(object sender, EventArgs e)
    5.     {
    6.         Response.ContentType = "text/plain";
    7.  
    8.         try
    9.         {
    10.             email = Regex.Replace(Request.Form.Get("email"), @"[^\w@.-_]", "");
    11.             pass = Regex.Replace(Request.Form.Get("pass"), @"[^\w-]", "");
    12.  
    13.         }
    14.         catch { Response.Write("false"); return; }
    15.  
    16.         string returnstring = sqlClass.sql_login(email, pass);
    17.  
    18.  
    19.         if (returnstring == true)
    20.         {
    21.             Response.Write("true");
    22.  
    23.         }
    24.         else { Response.Write("false"); }
    25.     }
    Then the sql class goes something like this, and using..
    using System;
    using System.Data;
    using System.Web;
    using System.Web.UI;
    using System.Data.SqlClient;
    using System.Globalization;

    Code (CSharp):
    1.  public bool sql_login(string email, string pass)
    2.     {
    3.         //return "0";
    4.  
    5.         string myselectQuery = "SELECT * FROM table WHERE password = '" + pass + "' AND email LIKE '" + email + "'";
    6.    
    7.         SqlConnection myConnection = new SqlConnection(mainclass.sqlConnection);
    8.         SqlCommand myCommand = new SqlCommand(myselectQuery);
    9.         myCommand.Connection = myConnection;
    10.  
    11.         try
    12.         {
    13.             myConnection.Open();
    14.         }
    15.         catch
    16.         {
    17.             if (myCommand.Connection.State == ConnectionState.Open) { myCommand.Connection.Close(); }
    18.             myCommand.Dispose();
    19.             return false;
    20.         }
    21.  
    22.         SqlDataReader reader = myCommand.ExecuteReader();
    23.  
    24.         try
    25.         {
    26.             if (reader.HasRows)
    27.             {
    28.                 reader.Read();
    29. // it will have rows if the persons login matchaes another in the database..
    30.  
    31.            
    32.                 string getcolumpass = reader["password"].ToString();
    33.                // do whatever you want here if you want to get more data about the user to send back.
    34.  
    35.                 myConnection.Close();
    36.                 return true;
    37.             }
    38.             else
    39.             {
    40.                 myConnection.Close();
    41.                 xend = false;
    42.             }
    43.  
    44.  
    45.         }
    46.         catch (Exception ee)
    47.         {
    48.             // do something
    49.         }
    50.  
    51.         finally
    52.         {
    53.  
    54.             if (myCommand.Connection.State == ConnectionState.Open) { myCommand.Connection.Close(); }
    55.             myCommand.Dispose();
    56.             if (!reader.IsClosed)
    57.             {
    58.                 reader.Close(); reader.Dispose();
    59.             }
    60.         }
    61.         return false;
    62.     }
    You will have to learn however how to setup your sql database and create tables, plenty of help on the internet for this..

    But this will give you an idea how its done, or one way to do it on a windows platform, its very simple to do this, and is not that complicated unlike what other people say, depending on what you use may make that job easyer or harder however, but i find this way to be very simple and quick for development..

    Please note this is just an example on how to use this technoligy, in practice you would have to do something similar but diferent, depending on weather you plan to have one dedicated game server, weather your planning to allow other people to run servers, the servers need to verify the person, so there are extra steps you need to take in order to achive this..
     
    Last edited: Feb 16, 2016
  38. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    <?PHP
    $user = $_REQUEST['user'];
    $pass = $_REQUEST['pass'];
    $con = mysqli_connect("fdb3.awardspace.net","?????","?????") or die ("Could not connect: " .mysqli_errno());
    if(!$con)
    die("Could not connect: ".mysqli_errno());
    mysqli_select_db($con , "2055720_admin") or die ("could not load the database: ".mysqli_errno());
    $check = mysqli_query($con , "SELECT * FROM acconts WHERE user = ".$user."");
    $numrows = mysqli_num_rows($check);
    if($numrows == 0)
    {
    $pass = md5($pass);
    $ins = mysqli_query("INSERT INTO 'accounts'('id','user','pass') VALUES ('' , '".$user."' , '".$pass."');");
    if($ins)
    die ("successfully created user!");
    else
    die("ERROR: ".mysqli_errno());
    }
    else
    {
    die ("user already exists!");
    }
    ?>

    this is the php code for registering an account, how could this work? I did a little research on the topic, and based this code of someone elses design and modified it a bit. When i type in the url for the php i get warnings, then an error. At the end if all works well it is sopposed to display:
    successfully created user!).
     
  39. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    wait a minute, I did something, I am going to test an updated form of that code, hope things work. I did something to the register php, the error is gone. I will now also work on the login php.
     
  40. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    I JUST MADE THE CREATE ACCOUNT HALF OF THE LOGIN SCREEN!!!! IT WORKS!!!
    now to work on the login half.
     
  41. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    <?PHP
    $user = $_REQUEST['user'];
    $pass = $_REQUEST['pass'];
    $con = mysqli_connect("fdb3.awardspace.net","?????","????") or die ("Could not connect: " .mysqli_errno());
    if(!$con)
    die("Could not connect: ".mysqli_errno());
    mysqli_select_db($con, "2055720_admin") or die ("could not load database". mysql_errno());
    $result = mysqli_query($con , "SELECT * FROM accounts WHERE 'user' = ".$user."");
    $numrows = mysqli_num_rows($result);
    if($numrows == 0)
    {
    die ("username does not exist");
    }
    else
    {
    $pass = md5($pass);
    while($row = mysqli_fetch_assoc($check))
    {
    if ($pass == $row['pass'])
    die("login successful");
    else
    die("inccorect password");
    }
    }
    ?>

    I am getting an error on line 9 that is causing the login half of the GUI to fail, giving me error:
    Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /srv/disk12/2055720/www/???????????????.dx.am/login.php on line 9

    can someone help me with this problem? after it is solved I believe the login gui will be functional. also, the client keeps telling me "username does not exist" even though the account was sucessfully created.

    here is the code to the register php:

    <?PHP
    //Code appears to be functioning, do not edit unless needed!!!
    $user = $_REQUEST['user'];
    $pass = $_REQUEST['pass'];
    $con = mysqli_connect("fdb3.awardspace.net","???","???") or die ("Could not connect: " .mysqli_errno());
    if(!$con)
    die("Could not connect: ".mysqli_errno());
    mysqli_select_db($con , "2055720_admin") or die ("could not load the database: ".mysqli_errno());
    $result = mysqli_query($con , "SELECT * FROM accounts WHERE 'user' = ".$user."");
    $numrows = mysqli_num_rows($result);
    if($numrows > 0)
    {
    $pass = md5($pass);
    $ins = mysqli_query("INSERT INTO 'accounts'('id','user','pass') VALUES ('' , '".$user."' , '".$pass."');");
    if($ins)
    die ("successfully created user!");
    else
    die("ERROR: ".mysqli_errno());
    }
    else
    {
    die ("user already exists!");
    }
    ?>
     
    Last edited: Feb 16, 2016
  42. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    Can someone help me with the final steps to make this login GUI functional?
    the codes for the login and registration phps are in my previous reply. The database is also opperational, but the client side (the login screen) does not seem to function. Is there any errors in my phps that need to be fixed? if so, how can I fix them?
     
  43. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    question, the timing in the forum is diferernt than my computers, are the people in this forum mainly from the west coast or east coast? I am in the east coast.
     
  44. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    I would like to thank everyone who posted these helpful things in this thread. I am currently studying on php, mysqli, and the other things I need to build this login system. This has proven to be a difficult task and I would like to show my thanks by one day making a tutorial on the login and registration system (that is if it works). If it happens to work, then I will create a new thread explaining how it works, and how to make your own login system.

    I have tested the account creation php, and it seems to be working fine.
    The login php also seems to be working fine, but may have errors. If it does I should be able to fix them.
     
    Last edited: Feb 16, 2016
  45. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    Isn't the method like this unsafe because you're storing MYSQL login and password inside of the code, which can be easily reverse engineered?
     
  46. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    not sure, the only security measure I have is that all the data is not stored on the client. The database is on the web. So unless you know the url and password to my database you can't really get into the database to hack it. Correct me if I am wrong. The login half of the game is also NOT functioning, however I can create an account. I said I would release a tutorial on how the login system works, but it does not seem to be functioning as of now. Also the create an account half is only functioning somewhat. I am going to look at the codes for errors, but if I can't find out what is wrong I will post here for help.
     
    Last edited: Feb 17, 2016
  47. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    <?PHP
    //THIS IS THE CURRENT REGISTER SCRIPT. DO NOT EDIT UNLESS NEEDED!
    $user = $_POST['user'];
    $name = $_POST['name'];
    $pass = $_POST['pass'];
    $conn = mysqli_connect("fdb3.awardspace.net","????","????");
    if(mysqli_connect_errno()){
    die("Failed to connect to MySQL: " . mysqli_connect_errno());
    }
    mysqli_select_db($conn , "????") or die ("Could not connect to db");
    $check = mysqli_query($conn, "SELECT * FROM accounts WHERE 'user' = ".$user."");
    $numrows = mysqli_num_rows($check);
    if($numrows == 0){
    $pass = md5($pass);
    $ins = mysqli_query($conn, "INSERT INTO accounts (user,name,pass) VALUES ('".$user."','".$name."','".$pass."')");
    if($ins)
    die("Successfully created account");
    }
    ?>

    That is the code for php register script. However on the client, when the user creates an account, the info appears blank on the database. Is this a problem with the client? or is this script messed up? How do I fix this?
     
    Last edited: Feb 17, 2016
  48. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    While the MySQL data isnt stored inside of Unity code, its connection IP, username and password are.

    Through such sensitive data, MySQL data can be obtained. Or even exploited (e.g more coins, damage etc.)
     
  49. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    No @elmar1028 you're incorrect. As long as the client does not have direct access to the database it's not unsafe. Making sure the client doesn't have direct access is up to the developer though.
     
  50. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    can someone help me with the code I posted above? It does not work as expected, when the account is registered it appears in the database blank.