Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Sessions to handle Multiple Servers

All,

I have a PHP5 web application written with Zend Framework and MVC. This application is installed on 2 servers with the same setup. Server X has php5/MySql/Apache and Server Y also have the same. We don't have a common DB server between both the servers.

My application works when accessed individually via https on Server X and Server Y. But when we turn on load balancing and have both servers up, the sessions get lost.

How can I make sure my sessions persist across servers? Should I maintain my db on a third server and write sessions to it? IF so, what's the easiest and most secure way to do it?

Thanks

like image 693
Jake Avatar asked Aug 13 '10 21:08

Jake


People also ask

How do you maintain a session across multiple servers?

If you are deploying application on more than one server, you should use "Clustering". Application servers are able to handle this scenario using "session replication". With session replication, each server will have a copy of the active users session.

How many sessions can PHP handle?

1000+ sessions can still be perfectly handled by standard PHP file based sessions. If you notice that is getting a problem, you can exchange the session backend easily. There are pluggable session handlers for memcached or other memory or database based storage systems.

Is PHP session server side?

Yes, the session data is on the server only.

Does PHP support session management?

PHP's session manager is adaptive by default currently. An adaptive session manager bears additional risks. When session. use_strict_mode is enabled, and the session save handler supports it, an uninitialized session ID is rejected and a new one is created.


1 Answers

memcached is a popular way to solve this problem. You just need to get it up and running (easy) and update your php.ini file to tell it to use memcached as the session storage.

In php.ini you would modify:

session.save_handler = memcache
session.save_path = ""

For the general idea: PHP Sessions in Memcached.

There are any number of tutorials on setting up the Zend session handler to work with memcached. Take your pick.

like image 60
jasonbar Avatar answered Oct 20 '22 07:10

jasonbar