4 web servers how can i get session

hi all:
I am sorry my englist is poor. I am a chinese. this is my first post thread at sitepoint. then I have some question to ask.

now there are 4 web servers.
example: 192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4
website is http://xxx.xxx.com

the user browse http://xxx.xxx.com ,then DNS point to an web servers.(example:192.168.1.2) after a while the DNS point to another (example:192.168.1.4)

usuallly I save the value of SESSION when user is loging
like this

$_SESSION['user'] = $user 

but the DNS will point another web servers ,and this the value of SESSION will not find …

can you help me how i do… let me get the value of session

感谢大家。

The default session storage mechanism for php is to store the data on the local filesystem. So when you switch webservers, this file/data is no longer there as you are on a different physical machine.

You can however, create a session handler using a shared resource (usually a database) available to all the servers.

See [fphp]session_set_save_handler/fphp. :wink:

Off Topic:

Ah, it’s good to see Jake agrees with me. I’m in good company.

Multiple servers always cause so many problems :lol:

Well, I’d personally have set it up differently, so that one server handles the MySQL, one server handles client-side files (images, CSS etc) and one handles PHP requests. However, I’ve never used multi-server setups so it may not be that simple.

So, as to your problem. Session variables are, by default, stored in files on the server they were set on, so you will have difficulty accessing those values across servers. You do not, however, need to use default session handling.

Take a look at Session_set_save_handler(). You can set up PHP to save session data to a database instead of on the server. The database can be connected to remotely, so you have all of your data stored in a central place accessible from all servers :slight_smile:

There are comments on the above manual page which may assist you with saving session data to the database.

Edit:

@Anthony - Scooby-doo criminal voice: Whyyy you little… :stuck_out_tongue:

think you
I found some people using “token”
user will get a “token” code when login success (the “token” will be save to a database )
now I want to know how can I get the token. how to create it or using some method.
thx.

will,if save session data to a database. how to know the value is timeout when user close browse not even they had not to logout from website …

When saving th session data to a database, you can save the datetime of the login to.

Then you can check from time to time if the login time is more than say, 20/30 minutes. If so, logout.