- Home /
help with the WWW Class
Ok so I am making a login script that queries a database
it sends of the username and password in a WWWFORM via WWW(url, WWWForm);
how would I go about getting a return ie in php (return true; or return false;) depending on if the details are correct or not
also would this be a practical way about doing it?
Another question I guess I could ask while Im on here that companies it is, if I logged the user in like the above, and in the mysql user table have an isloggedin field which is set to 1 and every few minutes send out another www() to remind the server were still online then use a cron to run another php file every 5 minutes setting isloggedin to 0 that way itll only keep the isloggedin set to 1 if theyre actually on
(so they can log in at the same time, also potentially making a friends list?)
would the above be impractical or out of the question full stop in unity?
the database will be on a dedicated server and online gameplay will be no more than 6 players per match and the game server will be hosted via client only the lobby master server will be on our dedi box
anyway long question but hopefully someone can give me some advice or point me in the right direction!
thanks in advance
Answer by DaveA · Feb 03, 2011 at 06:11 PM
That all sounds doable.
In your php, simply echo some sort of code, eg:
(after checking db) if (user_ok) echo "ok"; else echo "no";
or use numbers or some other cryptic code if you worry about hacks.
In the www.text that comes back, you should see that "ok" or "no"
You can then periodically 'ping' the server with a keep-alive script. You might want to use a session id. In fact, you could return the session id if user is 'ok' (instead of just saying 'ok'), or 0 if not ok. Then have the periodic ping send back that session id so you can set the 'online' flag in the db.
with the session, would it be easier to generate a random number with c# then send it with the username and password and store it all in one go then ive only got to worry about sending back a single int and use a switch for bad username/password/isloggedin etc as for the "ping" you wouldnt say it wouldnt make the game laggy sending off a www "ping" every 2$$anonymous$$utes? also one more question, would you suggest logging out the current player when someone else logs into the same account OR telling the 2nd person logging in that there is already someone playing on that account
Random number: either way, I've done both. If you generate it 'locally' you can also generate an 'authentication' datum too (by encrypting the first number with some known-only-to-you value. Then the php can validate that by running the same encryption and compare what was sent to what was done on the server. If different, someone's trying to hack it. Ping every 2 $$anonymous$$utes shouldn't hurt at all, since you're really just sending a very small 'GET' request and don't even have to process the return. I would probably go with telling the 2nd person that someone is already logged in.
Either case has the potential of annoyance: in the first case, if you log someone out, it's cool if they are the same person, new machine, but otherwise, it's a denial of service, rather rude. In the second, if someone is not legitimately logged in, they could also be denied service. You may want a 'should we log the other session out' option.
thank you for your words of wisdom dave, seeing as you seem to know a few amount about the www class maybe i pester you once more in asking how would i go about returning data other than text, i dont know if i was to return back an array for example to see whos online i did have a quick look at the highscores example on unify but if i remember rightly .data is obsolete am i right?
looking at the other potential variables on the documentation im gonna guess itd probably be bytes? but again it doesnt give much information or usage example
thanks for everything so far it has been helpful!
On this I'm not sure. Worst case you could use one of the ascii encodings for data, which bloat the size but at least are still text (you'd pack it in php and unpack it in Unity). But it may be that you can send byte data and just treat it as such. I don't know if any filter is done (say, end-of-line conversion or such). I would guess not. Worth a try. Also I know people have used JSON with WWW, for what that's worth. If the amount of data is small just use text with a compact format. I've used X$$anonymous$$L, but I wouldn't call it compact by any means.
Your answer
Follow this Question
Related Questions
How Do i check for Duplicate username in database 4 Answers
indie db hosting woes 1 Answer
Inventory with a Database 0 Answers
Unity Null Reference Error 1 Answer