Welcome, Guest. Please login or register.

Author Topic: Sessions in PHP+MySQL (very confused!)  (Read 4764 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline Nycran

  • Newbie
  • *
  • Join Date: Apr 2006
  • Posts: 17
    • Show all replies
    • http://www.dream-fusion.com
Re: Sessions in PHP+MySQL (very confused!)
« on: May 02, 2006, 08:04:36 AM »
Hey Moto,

Ok, here's what you need to understand.  When someone is playing your game, their web browser will not maintain a permanent connection to your server.  Instead, every time they click on a link they will make a brief connection, send and receive some information, and then close the connection.

This creates a problem because to maintain a history for each player (ie, to know what they previously did in the game), you will need to be able to identify the user each and every  time they connect.  

To solve the problem, you do this:

* You ask the user to login, ie, they enter an email address and a password and then hit a submit button to send the info to your server.

* PHP then reads the email and password and validates it against some kind of database.

* If their login is correct, you create some kind of unique ID in PHP - this could be a random sequence of numbers or characters or both, and then you write this uniqueID to a cookie in their browser.

* From then on, every time the user clicks on a link you can read your cookie, which is sent automatically by the browser to the server with each request.  Then you know who they are, so you can make your game react accordingly.

If you need an actual code sample of how to do this you can email me via nycran at yahoo dot com

Cheers.
 

Offline Nycran

  • Newbie
  • *
  • Join Date: Apr 2006
  • Posts: 17
    • Show all replies
    • http://www.dream-fusion.com
Re: Sessions in PHP+MySQL (very confused!)
« Reply #1 on: May 03, 2006, 09:26:33 AM »
Hey Moto, glad that you've got it going and glad I could help.

In my experience validating the user with each page request is not a significant overhead at all.  Any database worth a grain of salt (SQL Server, Oracle, MySQL, Postgres) with an indexed users table will perform this query in microseconds, even if there's hundreds of thousands of records.  I manage databases at work with over 40 million records and we have no trouble at all.  Queries that contain 20 correlated subqueries... now THEY create overhead! :-D

Cheers,
Andy.
 

Offline Nycran

  • Newbie
  • *
  • Join Date: Apr 2006
  • Posts: 17
    • Show all replies
    • http://www.dream-fusion.com
Re: Sessions in PHP+MySQL (very confused!)
« Reply #2 on: May 03, 2006, 01:19:41 PM »
Quote
What's a correlated subquery? My guess would be where you run a query to find a value from the database (e.g. query on session ID to find the user's name) then feed that value back in to another query (e.g. query on username to get the user's status in the game)


Spot on.  Consider the following:

SELECT username, password, (SELECT MAX(id) FROM userlog WHERE username = users.username)
FROM users

The second select is correlated with the first via the username = users.username join.  You can go nuts with these kinds of queries, correlating all over the show, but it does come with a performance cost.