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

WWW Help

Discussion in 'Scripting' started by nickavv, Apr 2, 2008.

  1. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    Hey there all. I have a log file which records successful/unsuccessful logins and their data when someone logs in. If I do it directly with the PHP script, it works fine, but when doing it though Unity using the following code, nothing is added to the log file. How would I change this to work as the PHP file does. I also have questions about getting the user's data back from PHP after I get this working, but that's another story.

    Code (csharp):
    1. function LogIn () {
    2.     var form = new WWWForm();
    3.     form.AddField ("myusername", WWW.EscapeURL(userName));
    4.     form.AddField ("mypassword", WWW.EscapeURL(passWord));
    5.     logIn = WWW(baseURL, form);
    6.     yield logIn;
    7.     if (logIn.error) {
    8.         errorArea = "An error occured while logging in: " + logIn.error;
    9.     } else {
    10.         loggedIn = true;
    11.     }
    12. }
    Thanks guys!
     
  2. VICTOM

    VICTOM

    Joined:
    Aug 28, 2005
    Posts:
    233
    You need to do some voodoo and make a broadcast function call from the html .js and have a listener function waiting for the broadcast inside of Unity.

    You can trigger that broadcast event ether via a web page .js function or from a Unity function. Fun stuff!
    Good Luck.


    Cheers,
     
  3. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    Well, this is the format that the log in page wants:

    index.php?myusername=x&mypassword=y

    So couldn't I just use the baseURL variable I have and tell it to add

    "?myusername="+userName+"&mypassword="+passWord"

    to baseURL and send that somehow?
     
  4. VICTOM

    VICTOM

    Joined:
    Aug 28, 2005
    Posts:
    233
    Out going events are simple - sure that otta work, just try it ;)

    It's the info inbound to Unity that needs the broadcasting/listener setup.

    Cheers,
     
  5. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    ahhhh, I see now. :p Thanks for the help, I'll try it out.
     
  6. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    Argh, okay, this isn't working. I tried it like this:

    Code (csharp):
    1. logIn = WWW(baseURL + "?myusername=" + userName + "&mypassword=" + passWord);
    2.     yield logIn;
    3.     if (logIn.error) {
    4.         errorArea = "An error occured while logging in: " + logIn.error;
    5.     } else {
    6.         loggedIn = true;
    7.     }
    and it didn't work. Then I tried it with an actual complete URL there instead of the code to build one, and that didn't work either.
     
  7. VICTOM

    VICTOM

    Joined:
    Aug 28, 2005
    Posts:
    233
    Can you get the log-in to work via typing the info into the browser as a url?

    And is the error msg printing? - I only see you setting the var not the print statement.

    How would you like Unity to reply with "Argh, okay, this isn't working."? I need to buy a clue, eh? :p

    Gotta give better error messages else no one can help you.

    Cheers,
     
  8. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    :p, okay, that was a little foolish of me. You're right about the error thing, as I was going to implement an error thing into the GUI and forgot. :p Let's see here...

    o_O No errors. And yeah, typing it right into the URL works fine. This code is basically copied and pasted straight from my Sky Sky Panic 3d highscore submission code, so I really don't know why it wouldn't work, but I have a feeling that it will be something really obvious like "I changed a variable in Unitron, but not in the Inspector" (which I already checked for btw. :p)
     
  9. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    How are you checking for your URL parameters in PHP? The default WWW class operation in your last script is a get call so in your PHP you should be using $HTTP_GET_VARS (or an equivalent). If you're using $HTTP_POST_VARS (or an equivalent) then your WWW operation will complete just fine (your PHP file was successfully called) but no data will be stored (your script won't see any post URL parameters because they're really get URL parameters).
     
  10. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    In the PHP I'm using $_GET['myusername'] and $_GET['mypassword'].
     
  11. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    To help with some debug information I'd have your PHP script do whatever database work it needs to do, then use echo() to write out a return message (example: the name and password it received from the URL). Then in your Unity code you can (for now) detect that no error occurred and display the return string after/before you set loggedIn to true.

    This will let us see what the URL the parameter values are, at least as received by the PHP script.
     
  12. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    I did a print (baseURL + "?myusername=" + userName + "&mypassword=" + passWord); in Unity, and got a URL, which I then put into my web browser. The results got written to the log.txt file, as expected. When I clicked the Log In button in Unity though, nothing was added to the log. That is what you wanted me to try, right? >.> I feel so completely ignorant when doing things like this.
     
  13. hai_ok

    hai_ok

    Joined:
    Jun 20, 2007
    Posts:
    193
    I'm doing the same thing.

    I've tried to escape the url (which breaks the : in after http btw).

    I've stored the base url and the "&variable="+value stuff separately and combined it with the escaped url.

    I've appended as in ("http://"+url+parameters)

    And I've printed the results back to the console every time.

    I was up till 2:00 AM last night (I don't like to stop until something works).

    And it only now occurs to me in mid-post, that I forgot to append the leading "?" before the parameters.

    sigh...

    Guess I should have looked at the forums a bit longer. I'm at work now. I'll try to remember to post when I get home.

    I was going to ask for help.

    Now I need to ask for a nap.
     
  14. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    That's definitely _not_ what I wanted you to do. ;) :D What you did above is print out the string you are attempting to send, I want you to have your PHP script return a string to your Unity content, then within Unity print the returned string so you can verify what is actually received by your PHP script.

    Add this to your PHP file:

    Code (csharp):
    1. $name = $_GET['myusername'];
    2. $pass = $_GET['mypassword'];
    3.  
    4. // do your database stuff here, then...
    5.  
    6. echo("Hey, the name received by PHP was " . $name . " and the password was " . $pass");
    Then in your Unity file, after you detect that the WWW call is done and error free (either just before or just after you set loggedIn to true), add this:

    Code (csharp):
    1. print(logIn.data);
    If you do that your PHP will be "notifying" your Unity content about the values it's actually received (your efforts are only verifying what you are attempting to send). From there you can see if your sent data matches the received data or not.

    Note: you will of course remove the script additions I'm suggesting once you sort this out, they are temporary additions only as we need them to help with the debugging effort.
     
  15. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    Ohhhhh, I get it now. Well, I did as you said, here's what it returned:

    Code (csharp):
    1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    2. <html>
    3. <head>
    4. <meta http-equiv="REFRESH" content=0";url=http://swiftphp.com/blog/index.php">
    5.  
    6. <script type="text/javascript">
    7. var sc_project=2574433;
    8. var sc_invisible=1;
    9. var sc_partition=25;
    10. var sc_security="5366add3";
    11. var sc_remove_link=1;
    12. </script>
    13. <script type="text/javascript" src="http://www.statcounter.com/counter/counter_xhtml.js"></script><no
    14. script><div class="statcounter"><img class="statcounter" src="http://c26.statcounter.com/2574433/0/53
    15. 66add3/1/" alt="web site analytic" /></div></noscript>
    16.  
    17. <meta content="text/html; charset=ISO-8859-1"
    18.  http-equiv="content-type">
    19.      <title>Web</title>
    20.      </head>
    21.      <body>
    22. <script type="text/javascript">
    23. <!--
    24. window.location = "http://swiftphp.com/blog/index.php"
    25. //-->
    26. </script>
    27.      
    28.  
    29.      </body>
    30.      </html>
    Absolutely none of the echo lines I have in. :(
     
  16. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    You sure you're accessing the right url?
     
  17. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    I still don't think you're getting my request at all. :p I'm asking you to do this:

    - In Unity, craft a PHP file URL to store data (including your two URL parameters), then call that file using the WWW class

    - In your PHP file, do your database storage work* and then the only data you write out using echo should be a simple string that shows the URL parameters received by the PHP script

    *Or maybe even comment out all your database code for now and just receive the URL parameters and immediately return them as a string. The problem here is that you're sending data and it seems that data isn't being received so we have to sort out whether the PHP script is getting the URL parameters properly.


    I have no clue why your output is an entire web page and as I get further into this problem with you I'm only getting more confused as to what it is you're doing. :(


    Edit: and in order for us to help it may be wise for you to post your PHP script (either the text of it or the file itself) for us to see. If you do that then please remove the name and password you use to access your database.