OK, so I have a completely rare unique load-balanced PHP website script. Bummer - this was not load balancing. Now we are starting to get problems ...
Currently, the only problem is with PHP sessions. Naturally, no one thought about this problem at first, so the configuration of the PHP session remained by default. Thus, both servers have their own small table of session files, and grief is the user who receives the next request thrown to another server because he does not have the session that he created on the first.
Now I read the PHP manual on how to solve this situation. There I found a nice session_set_save_handler() function. (And, coincidentally, this section is on SO). In addition, I will have to call this function on all pages of the website. And the developers of future pages would have to remember that they’ve been calling all this time. He feels awkward, not to mention the fact that he probably violates the top ten best coding methods. It would be much nicer if I could just flip some global configuration option, and Voilà - sessions that are all magically stored in a database or memory cache, or something like that.
Any ideas on how to do this?
Added: To clarify - I expect this is a standard situation with a standard solution. Fyi. I have a MySQL DB. Surely there should be some kind of ready-to-use code that solves this? I can, of course, write my own material to save the session, and the
auto_prepend option specified by
Greg seems promising - but it will be like rethinking the wheel.: P
Added 2: Load balancing is based on DNS. I'm not sure how this works, but I think it should be something like
this .
Added 3: OK, I see that one of the solutions is to use the
auto_prepend option to insert a call into
session_set_save_handler() in each script and write my own DB client, possibly to make
memcached calls for better performance. Fair enough.
Is there a way that I could not encode all of this at all? Like some famous and proven PHP plugin?
Added a lot, much later: In the end, I went: How to properly implement user session persister in PHP + MySQL?
In addition, I just turned on the session handler manually on all pages.