PHP sessions in a load balancing cluster - how?
You could set PHP to handle the sessions in the database, so all your servers share same session information as all servers use the same database for that.
A good tutorial for that can be found here.
Handling $_SESSION object in a load-balanced setup, PHP
Look into either memcache (Is it recommended to store PHP Sessions in MemCache?) or REDIS (https://joshtronic.com/2013/06/20/redis-as-a-php-session-handler/).
There is a good tutorial on setting up memcache on Ubuntu at https://www.globo.tech/learning-center/php-memcached-instances-ubuntu-16/. Which also covers using haproxy as a load balancer (although you may already a solution).
Perhaps have a read of https://blog.newtonhq.com/session-handling-for-1-million-requests-per-hour-68cdece15030.
How To maintain Session over Load Balancer?
i can think of these two methods for this purpose.
- Use a clustered web application server where the session are available for all the servers
- Use IP level information to maintain affinity between a user and a server
What is the best way to handle sessions for a PHP site on multiple hosts?
Database, or Database+Memcache. Generally speaking sessions should not be written to very often. Start with a database solution that only writes to the db when the session data has changed. Memcache should be added later as a performance enhancement. A db solution will be very fast because you are only ever looking up primary keys. Make sure the db has row locking, not table locking (myISAM). MemCache only is a bad idea... If it overflows, crashes, or is restarted, the users will be logged out.
Are there problems using PHP sessions in a server cluster?
Yes this is possible, you need to store your sessions in a central location like a database though. This is pretty simple and just requires you to make some changes to session_set_save_handler - there's a good example of the process you need to follow here
Related Topics
PHP Session for Tracking Unique Page Views
Replace Only First Match Using Preg_Replace
Using Regex to Filter Attributes in Xpath with PHP
Check If Url Has Certain String with PHP
Simple PHP Form: Attachment to Email (Code Golf)
How to Set Cron Job Url for Codeigniter
PHP Strtotime "Last Monday" If Today Is Monday
Using Mx Records to Validate Email Addresses
Get Filename of File Which Ran PHP Include
What Is Null Coalescing Assignment = Operator in PHP 7.4
How to Put JavaScript Variable in PHP Echo
Fatal Error - 'Mongo' Class Not Found
How to Query All the Graphql Type Fields Without Writing a Long Query