How to Manage a Single PHP5 Session on Multiple Apache Servers

How to manage a single PHP5 session on multiple apache servers?

You'll have to use another session handler.

You can:

  • build your own (see session_set_save_handler) or
  • use extensions that provide their own session handler, like memcached

Using of Session Database on multiple Apache Servers

It appears the problem you are having can be solved using distributed sessions.

Using memcached you can provide a central point for all session data that any connected server can share.

If you are using linux, the below code demonstrates how commenting out local file session handling, and replacing it with memcache can allow you to share session data.

  ~$ cat /etc/php5/apache2/php.ini | grep -i session 
[Session]
;session.save_handler = files
session.save_handler = memcache
session.save_path = "tcp://127.0.0.1:11211"

For an in depth explanation visit: http://bakery.cakephp.org/articles/rynop/2010/09/10/using-memcached-to-run-your-sessions

Transfer session across server in PHP

Wouldnt it be easier to just store session data in a shared directory?

Alternatively you can store it in a database.

How to get my session to write to apache

Try changing your session save path in your php config file, /tmp is a good location.

php.ini

session.save_path = /tmp

http://www.php.net/manual/en/session.configuration.php#ini.session.save-path

Why cannot Apache handle multiple requests at the same time?

Apache can surely handle multiple requests at the same time, there is something surely going wrong within your apache configuration.

It depends on which version of Apache you are using and how it is configured, but a common default configuration uses multiple workers with multiple threads to handle simultaneous requests. See http://httpd.apache.org/docs/2.2/mod/worker.html for a rundown of how this works.

The reason why you are facing it is:
There is some lock somewhere - which can happen, for instance, if the two requests come from the same client, and you are using file-based sessions in PHP : while a script is being executed, the session is "locked", which means the server/client will have to wait until the first request is finished (and the file unlocked) to be able to use the file to open the session for the second user.

The requests come from the same client AND the same browser; most browsers will queue the requests in this case, even when there is nothing server-side producing this behaviour.



Related Topics



Leave a reply



Submit