Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How the Multiplayer game works with php based server..

Discussion in 'Multiplayer' started by Dinesh Balu, May 3, 2011.

  1. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    Hi...
    I'm working on a multiplayer game with have to use a php based server. I've learned something from the links (Server Side High Scores ,WWW) and tried it in a demo project as given below.

    Code (csharp):
    1.  
    2. var LabelText:String;
    3. var highscoreUrl="http://www.1site.net/php-example-page.php";
    4.  
    5. function Start()
    6. {
    7. LabelText=" ";
    8. getServerText();
    9. }
    10.  
    11. function getServerText()
    12. {
    13.    gameObject.guiText.text= "Loading Scores";
    14.      hs_get = WWW(highscoreUrl);
    15.      yield hs_get;
    16.      
    17.      if(hs_get.error) {
    18.          print("There was an error getting the high score: " + hs_get.error);
    19.      } else {
    20.         gameObject.guiText.text= hs_get.text; // this is a GUIText that will display the scores in game.
    21.      }
    22. }
    This code belongs to a gameobject. When execute the project, the text field is filled with complete source code of the php page. I don't understand how to use php server to my game.

    How the PHP server and unity game works together.

    can I test the PHP server - unity client communication in a single system.

    I want to test a demo project , in which two unity application communicates through the PHP server page without database.
     
  2. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    The problem is on your web server. It's obviously not executing the PHP if it's returning the source. If you try going to the url with a browser, you will see the exact same thing.
     
  3. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    this php page is not running in my localhost... It's a link page of the site "1site.com"...

    Tell me , what the following does..


    how will be the communication b/w the php server and the unity application...
     
  4. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    Once again, Unity can only retrieve what the web server provides. Unity isn't hacking into the server to steal the PHP source.
     
  5. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    If I'm using database in my game which located in the server...
    do I request server for the data in database (or)
    do I access the database directly by the connection with the server (or)
    do I read and decode the data that the server frequently provides in encoded form with data and some ID.
     
  6. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    You never grant direct access to your database. That's the purpose of PHP. However, if your web server is not properly configured to execute the PHP document then all you'll get is the source code.

    I don't understand why it's such a difficult concept. Like I said, go to the same URL with a web browser. You'll quickly see what's broken.
     
  7. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    me either...

    Let me get that straight... the unity application reads the data provided by the server and it checks that the data is belong to particular game ID and player ,then the it accepts the data.... this is how it should work right?...
     
  8. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    That sounds perfectly reasonable, but if your web server isn't executing the PHP script there's a bit of a problem.

    They say the third time is the charm so I'll try once more. Go to the url with a web browser, you will see there is a problem. You need to fix that, before you can worry about parsing the data returned.
     
  9. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    I don't understand... the url works fine... It's not developed by me...
    you meant the above url that I used to access right?
     
  10. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    That is not a url to a PHP script. It's a url to an HTML page describing how to configure your PHP script.
     
  11. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    Oh... I'm sorry... should I have Supporting server like Apache server installed in my system to access the php script. that php script is not running in my system.. it's running in my local network system.
     
  12. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    I change the url to "http://192.168.1.23/unity/display.php" this file is in my local network system with local IP 192.168.1.23.I have tested it in my browser, and it works,.... but not in my unity script,... it keeps displaying some source code...
     
  13. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    Code (csharp):
    1. hs_get = WWW(highscoreUrl);
    2. gameObject.guiText.text= hs_get.text;
    yes because you get the html/php syntax from the WWW and you attach that directly to the guitext.
    you will need to parse the received string in the format you need.
    like a webbrowser does when you look at a page, it doesn't show the text input it gets but it formats into a 'viewable' format.
     
  14. RodrigoSeVeN

    RodrigoSeVeN

    Joined:
    Jul 10, 2010
    Posts:
    15
    Just to check, is your php using 'echo' to send the values back? Anyways, i've been using "hs_get".data to catch the return. This gives me anything that the php sent using 'echo'. I'm dealing a lot with this and i had no problem with this part so far.
     
  15. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    The parsing is not necessary in this.
    As SeVen said variable text of class WWW return the data echoed by php script.

    My project works now..... I just saved the scene (before that I didn't save it ,not even once), then I close the editor and re opened it... It just works....
     
  16. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    Thanks Seven.. now I got it... it works

    I have to save the scene first...:D