Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Tutorial: Unity and PHP login script - simple but useful

Discussion in 'Multiplayer' started by xandeck, Jun 10, 2009.

  1. xandeck

    xandeck

    Joined:
    Apr 2, 2009
    Posts:
    563
    Strange... I have a similar code like yours, but I use a "for loop" to return the results in php (using echo).

    Like this PHP script:
    Code (csharp):
    1.  
    2.     $SQL = "SELECT * FROM users ORDER BY score DESC LIMIT 10";
    3.     $result_id = @mysql_query($SQL) or die("DATABASE ERROR!");
    4.     $total = @mysql_num_rows($result_id);
    5.  
    6.     for($i = 0; $i < $total; $i++){
    7.          $row = @mysql_fetch_array($result_id);
    8.          echo $row['score'] . " - " . $row['name'] . "\n";
    9.     }
    10.  
    Then I store all this in a simple string inside Unity, and it just works.

    Code (csharp):
    1.  
    2. //Javascript example code
    3. //The 'phpScript_Ranking' var will store the path to your php script
    4.  
    5. var ranking : string = "";
    6.  
    7. function GetRank(){
    8.     var form = new WWWForm();
    9.     form.AddField( "f_hash", hash);
    10.     var w = WWW(phpScript_Ranking, form);
    11.     yield w;
    12.     if (w.error != null) {
    13.         Debug.Log(w.error);
    14.         messages = w.error;
    15.         status = 0;
    16.     } else {
    17.         //Debug.Log("Got Rank");
    18.         //Debug.Log(w.data);
    19.         if (w.data == "notHash"){
    20.             messages = "hash wrong";
    21.         } else {
    22.             ranking = w.data;
    23.         }
    24.     }
    25. }
    26.  
    Then just display the ranking var inside some GUI... this just works fine.

    EDIT: just copying this scripts will not work, I just got the 'interesting' parts ;)
     
  2. crasyboy42

    crasyboy42

    Joined:
    Jun 29, 2010
    Posts:
    28
    stil i need help with getting the exp and level from the database
    i was thinking i could use this line of code insite the php script

    $_POST['exp'] = $exp;

    i get the exp level right from the database(i use the same code to get the stats for my stats page)
    but i can't sent i to unity

    please help :(
     
  3. xandeck

    xandeck

    Joined:
    Apr 2, 2009
    Posts:
    563
    How do you send the data to Unity? I do by echo PHP code.
     
  4. crasyboy42

    crasyboy42

    Joined:
    Jun 29, 2010
    Posts:
    28
    that part i cant do
    i want that unity DONT see the exp or level that i echo

    so i can use it in the stats screen :)
    (or someting)
    or isnt that posible??
     
  5. xandeck

    xandeck

    Joined:
    Apr 2, 2009
    Posts:
    563
    Hmm, I dont know what you mean.

    Your doubt is inside Unity or about PHP?
     
  6. crasyboy42

    crasyboy42

    Joined:
    Jun 29, 2010
    Posts:
    28
    both
    look i must get the exp and the level from the character from the database
    but if i echo somting insite php unity sees that in that textbox
    but i want that unity DONT see the level and exp but just send it to the stats screen witouth visible for the player

    this time i use google translate better i cant translate it srry :(
     
  7. xandeck

    xandeck

    Joined:
    Apr 2, 2009
    Posts:
    563
    If you dont want to display the info in the textbox, just dont send it to there, direct the php data to another variable.
     
  8. crasyboy42

    crasyboy42

    Joined:
    Jun 29, 2010
    Posts:
    28
    hmm i wil try that :)
    but i thought that everything yuo echo comes int he textbox? :)
     
  9. xandeck

    xandeck

    Joined:
    Apr 2, 2009
    Posts:
    563
    No, just in that example, you can direct to other variables :wink:
     
  10. crasyboy42

    crasyboy42

    Joined:
    Jun 29, 2010
    Posts:
    28
    if i get out these 3 i wil post s code :p
    if not
    i wil ask again for help :(
     
  11. wrm186

    wrm186

    Joined:
    Apr 4, 2010
    Posts:
    661
    "register" or "register online" and then brigs up a page and fill out the information required (how do you make a registration form also) then when there done they use there "username" and "password" and then start playing the game? Im sorry =/ im still new to all of this and im a noob when it comes to scripting. And then I was hoping I could make a chat box in the game as well. But I have no idea where to find a script for that. Any ideas? =/ hope you can help me. :?
     
  12. crasyboy42

    crasyboy42

    Joined:
    Jun 29, 2010
    Posts:
    28
    if you dont have a server for a mmo game you dont need a chat ;)

    and for the register part do you want a in game register or on a website
    on a website it is just php that isnt so hard on youtube you can find you answer and in game i am start working on that but no suc6 so far :(
     
  13. xandeck

    xandeck

    Joined:
    Apr 2, 2009
    Posts:
    563
    For a chat, its better if you have a dedicated server or something like that. You can do have a chat with PHP and Unity, but I think it could be a mess and you will have to sync every time to check for new chat messages... well, thats the way I think... :roll:

    Or not even a dedicated server, but a normal server made in Unity...
     
  14. crasyboy42

    crasyboy42

    Joined:
    Jun 29, 2010
    Posts:
    28
    smartfox server demo has a build in chat :)

    but it is hard to make a mmo my and my team have done it but it takes about 2 weeks only to get the server up and running (on a local machine)
     
  15. xandeck

    xandeck

    Joined:
    Apr 2, 2009
    Posts:
    563
    Yeah, smartfox is a good solution for low scale games, I made a little chat once, it is not very hard, we can even find some nice tuts
     
  16. grfxman

    grfxman

    Joined:
    May 28, 2009
    Posts:
    309
    It seems like there would be a problem if two people with the same name registered. I was wondering how you could use the operator in PHP to test for both name and password.

    I can't seem to get the Syntax right.

    Also whats up with the '" around the PHP variables, why are there 3 quotes?
     
  17. xandeck

    xandeck

    Joined:
    Apr 2, 2009
    Posts:
    563
    Where are you checking the "'"? It must be some verification in database.

    To check if 2 people registered you need to make your own security check for this, that script is not prepared for that. You can make, for example, another field into database to register/save who is online and then, when try to login, you can check this.

    You cannot do this in Unity, because you dont have a server in there, only the client.
     
  18. grfxman

    grfxman

    Joined:
    May 28, 2009
    Posts:
    309
    Actually I'm just trying to figure out the PHP Syntax to Change the Query statement to use Both the name and the password variable. If that one line checked both then the sample code would be fully functional. But I'm new to PHP and not sure how to write it.

    I was able to verify that PHP could use in the Query statement though.
     
  19. grfxman

    grfxman

    Joined:
    May 28, 2009
    Posts:
    309
    This line of PHP code :

    Code (csharp):
    1. $SQL = "SELECT * FROM scores WHERE name = '" . $nick . "'";

    Could Be :

    Code (csharp):
    1.  
    2. $SQL = "SELECT * FROM scores WHERE name = '" . $nick . "' AND  password = '" . $pass . "';";
    This would allow players to have the same name and chances are that their Password choice would be different from each other.

    A friend at a coffee shop turned out to be a PHP coder :)
     
  20. chineseboy

    chineseboy

    Joined:
    Sep 22, 2010
    Posts:
    2
    OH!this is very good tutoria!

    Thanks very much!
     
  21. CorruptedHeart

    CorruptedHeart

    Joined:
    May 4, 2010
    Posts:
    379
    Code (csharp):
    1. $SQL = "SELECT * FROM scores WHERE name = '" . $nick . "' AND  password = '" . $pass . "';";
    Could also be written as
    Code (csharp):
    1. $SQL = "SELECT * FROM scores WHERE name = '$nick' AND  password = '$pass';";
     
  22. delta11

    delta11

    Joined:
    Nov 23, 2010
    Posts:
    3
    Realy Good tut
    I have just one question how can I make the GUI Button and Textfields dissapear (once the login came back oke)
    Thnx in advance
     
  23. xandeck

    xandeck

    Joined:
    Apr 2, 2009
    Posts:
    563
    One solution I can think right now is: make a variable (like a boolean) and made it TRUE for showing the GUI... when the data returns from the PHP saying ok just make this variable false.

    Got it? ;)
     
  24. delta11

    delta11

    Joined:
    Nov 23, 2010
    Posts:
    3
    Yes got it :) ty works great
    Just one more Question the script that talks to the php has a error message
    Assets/Scripts/UnityPHP.js(38,30): BCW0012: WARNING: 'UnityEngine.WWW.data' is obsolete. Please use WWW.text instead
    I still pretty new with unity (and object-oriented programming) and I don't know how to rewrite this to make it work with that new methode
    ps any tips on how to star out the password (probaly something with stringlenght and storing the actuale letters in a different var)
     
  25. CorruptedHeart

    CorruptedHeart

    Joined:
    May 4, 2010
    Posts:
    379
    For the WWW.data, simply change all uses of that to WWW.text and it should work fine.

    For the password, there is actually a GUI method for displaying a password field.
     
  26. xandeck

    xandeck

    Joined:
    Apr 2, 2009
    Posts:
    563
    Yeah, thats right...

    For some reason, I'm not receiving emails saying about new posts in any topics I'm in o_O
     
  27. delta11

    delta11

    Joined:
    Nov 23, 2010
    Posts:
    3
    Yeah that's what I thought but turns out I was looking at the wrong line :oops:
    Sorry for the screw up ;)
     
    Last edited: Nov 23, 2010
  28. Zu01

    Zu01

    Joined:
    Aug 17, 2010
    Posts:
    95
    I got lost in 2 places. First, the first file you made, should that be saved as SCORES.db? and then the :
    var URL = "http://mywebsite/check_scores.php"
    There was no explanation on what to put there. so I just took out http, and changed to to the directory of my check scores.php file. That didn't work, any suggestions?
     
  29. wrm186

    wrm186

    Joined:
    Apr 4, 2010
    Posts:
    661
    Why is scrpiting so hard and confusing for me!!!! >_< I only like creating models. And (I'm not trying to scam just giving some free models that other people can use) go to my web site for free models I made if you guys would like. I'm still kinda new to it so sorry if they're bad or not good at all :(

    http://www.free3dmodels.webs.com/

    please tell me if there good or bad please? Thank you
     
  30. Zu01

    Zu01

    Joined:
    Aug 17, 2010
    Posts:
    95
  31. athanazio

    athanazio

    Joined:
    Nov 10, 2009
    Posts:
    26
  32. MayhemOfHell

    MayhemOfHell

    Joined:
    Jan 2, 2011
    Posts:
    1
    Considering this thread looks like it maybe one of the few in here and has just come back from the dead. Thought i would share a hard copy of this sort a setup with people. And also included the MD5 javascript version.

    View attachment $mdwUnityLoginScript(Using PHP).zip
    So attached you will find a single zip with everything inside of it.. three folders.

    MySQL = the members.sql file to import to your mysql database setup
    PHPServer = the files to be placed on your server/website
    Unity3D = A package containing the camera script, the script itself and the whole scene/project zipped as well.

    This is just a basic post for now, So if anyone has any questions. Feel free to ask. But otherwise more or less just shows a simple db search and all as the first example this thread started with.
     
  33. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Cool beans, Mayhem, I'll check it out later.

    I'm currently working on an implementation of the same concept, but integrating it with a WordPress install and the users table, with a custom plugin I wrote that requires users to pay a fee via paypal to create an user account on your WordPress site. I'll post it when I am finished.
     
  34. Zu01

    Zu01

    Joined:
    Aug 17, 2010
    Posts:
    95
    Log in requires a website? That kinda sounds bumb but no one mentioned that :(
     
  35. xandeck

    xandeck

    Joined:
    Apr 2, 2009
    Posts:
    563
    Hey Zu01, if you still have problems, let me know... I'm still not receiving any notifications email about my posts...
     
  36. wrm186

    wrm186

    Joined:
    Apr 4, 2010
    Posts:
    661
    Nah, there good man. I could never do that mammoth.
    I wouldn't even know how to texture it ha-ha. (I suck at texturing)
     
  37. ChurroLoco

    ChurroLoco

    Joined:
    Sep 20, 2010
    Posts:
    82
    I'm new to PHP so... why do you put in sql injection guards in the passwords if you are just going to MD5 it?... won't the MD5 just convert all the nastyness into a safe string anyway?
     
  38. xandeck

    xandeck

    Joined:
    Apr 2, 2009
    Posts:
    563
    Hello, sorry the delay on answer, I'm still not receiving any email confirmation in post replies.

    The Sql Injection function is to check another parameters and words besides MD5. MD5 its just encription. Check out the function and you will see. =)
     
    Last edited: Mar 21, 2011
  39. Zu01

    Zu01

    Joined:
    Aug 17, 2010
    Posts:
    95
    Its been a while, I scrapped some cahs together a bought a website! Negagames.com, still stuck though. People mentioned mysql, I downloaded it and my computer was just being screwy with it :(. I tried the original tutorial u put at the beginning, it doesn't work though, it lets me connect to negagames.com w/o a username at all. then, when i did negagames.com/checkscores.php, it said there was a problem because it couldn't find a crossdomain.xml file attached r something. Any ideas?
     
  40. xandeck

    xandeck

    Joined:
    Apr 2, 2009
    Posts:
    563
    The tutorial works =)
    If I get it right, you are having problems with crossdomain.xml, right?
    This is another problem... check the foruns, there are a lot of posts about this ;)
     
  41. Zu01

    Zu01

    Joined:
    Aug 17, 2010
    Posts:
    95
    Ok, I just have to tell you that... Im not the networking guy. It worked when I romeved the http:// and it worked, to a point ( have negagames.com/crossdomain.xml) then it workes no matter what!!! Is this a tutorial error r mine, this is what appears when i put in login

    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <HTML><HEAD>
    <TITLE>405 Method Not Allowed</TITLE>
    </HEAD><BODY>
    <H1>Method Not Allowed</H1>
    The requested method POST is not allowed for the URL /crossdomain.xml.<P>
    <HR>
    <ADDRESS>Apache/1.3.39 Server at negagames.com Port 80</ADDRESS>
    </BODY></HTML>

    also, just fyi im using webs as my host.
     
  42. xandeck

    xandeck

    Joined:
    Apr 2, 2009
    Posts:
    563
    Dont know what is happening in there o_O
    I still think is something related to the crossdomain.xml
     
  43. Zu01

    Zu01

    Joined:
    Aug 17, 2010
    Posts:
    95
    I searched but, it's all so confusing. I don't want to be a burden so i'll keep searching when I am on spring break (8th grade is rough)
     
  44. Lemon223

    Lemon223

    Joined:
    Nov 28, 2010
    Posts:
    11
    where do i have to place this piece of code?


    Code:
    INSERT INTO `scores` ( `id` , `name` , `password` )
    VALUES (
    NULL , 'xandeck', MD5( '1234' )

    i never worked with mysql before and just got the first step with alot of trying but can't find any way to place that somewhere... really want to learn this stuff :) started yesterday with php so i hope i can finish this tutorial so i can go on :)

    thanks in advance,

    Lemon,
     
  45. Lethal

    Lethal

    Joined:
    Jun 10, 2011
    Posts:
    3
    Thank you for this, but I am trying to make it so if their login information is correct the level will load if not it wont load. Seems like it's working if the host/data is connecting. Please help.

    The JS Unity Sided Code.

    Code (csharp):
    1. function Login() {
    2.     var form = new WWWForm(); //here you create a new form connection
    3.     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
    4.     form.AddField( "myform_nick", formNick );
    5.     form.AddField( "myform_pass", formPassword );
    6.     var w = WWW(URL, form); //here we create a var called 'w' and we sync with our URL and the form
    7.     yield w; //we wait for the form to check the PHP file, so our game dont just hang
    8.     if (w.error != null) {
    9.         print(w.error); //if there is an error, tell us
    10.     } else {
    11.         print("Login, Successful!");
    12.         Application.LoadLevel("Highway");
    13.         formText = w.data; //here we return the data our PHP told us
    14.         w.Dispose(); //clear our form in game
    15.     }
    16.  
    17.     formNick = ""; //just clean our variables
    18.     formPassword = "";
    19. }
     
  46. Lethal

    Lethal

    Joined:
    Jun 10, 2011
    Posts:
    3
    Can anyone help me make this for Mybb integration. :D would be so awesome if you could thanks!
     
  47. Bờ Cát trắng

    Bờ Cát trắng

    Joined:
    Feb 15, 2011
    Posts:
    2
    Hi Sandeck!
    It is very useful. Your solution is very good. Thanks!
    But I have a other solution to access database MS SQLServer. We can make a socket application to listening some request from Unity. This application can be call method of webservice, then it return data to Unity. So, we can do everythings with database.
     
  48. zhapness

    zhapness

    Joined:
    Apr 10, 2011
    Posts:
    58
    I Had the crossdomain.xml problem too when i tried this in a web player, I fixed it with, putting Application.dataPath before the php path, and the php path has to be from the folder your webplayer is in,
    so:
    Line 5: var URL = "/check_scores.php";

    Line 28: var w = WWW(Application.dataPath + URL, form);
     
    Last edited: Aug 24, 2011
  49. zhapness

    zhapness

    Joined:
    Apr 10, 2011
    Posts:
    58
    This helped me allot, thanks for this!
     
    Last edited: Aug 13, 2011
  50. SEG-Veenstra

    SEG-Veenstra

    Joined:
    Sep 3, 2010
    Posts:
    326
    I just want to thank you for the script! I was looking for something like this and got kind of desperate!