title: PHP sessions in a load balancing cluster - how?
body: <p>OK, so I've got this totally rare an unique scenario of a load balanced PHP website. The bummer is - it didn't used to be load balanced. Now we're starting to get issues...</p>
  
  <p>Currently the only issue is with PHP sessions. Naturally nobody thought of this issue at first so the PHP session configuration was left at its defaults. Thus both servers have their own little stash of session files, and woe is the user who gets the next request thrown to the other server, because that doesn't have the session he created on the first one.</p>
  
  <p>Now, I've been reading PHP manual on how to solve this situation. There I found the nice function of <code>session_set_save_handler()</code>. (And, coincidentally, <a href="https://stackoverflow.com/questions/76712/what-is-the-best-way-to-handle-sessions-for-a-php-site-on-multiple-hosts">this topic</a> on SO) Neat. Except I'll have to call this function in all the pages of the website. And developers of future pages would have to remember to call it all the time as well. Feels kinda clumsy, not to mention probably violating a dozen best coding practices. It would be much nicer if I could just flip some global configuration option and <em>Voilà</em> - the sessions all get magically stored in a DB or a memory cache or something.</p>
  
  <p>Any ideas on how to do this?
  <hr>
  <strong>Added:</strong> To clarify - I expect this to be a standard situation with a standard solution. FYI - I have a MySQL DB available. Surely there must be some ready-to-use code out there that solves this? I can, of course, write my own session saving stuff and <code>auto_prepend</code> option pointed out by <a href="https://stackoverflow.com/questions/994935/php-sessions-in-a-load-balancing-cluster-how/994988#994988">Greg</a> seems promising - but that would feel like reinventing the wheel. :P
  <hr>
  <strong>Added 2:</strong> The load balancing is DNS based. I'm not sure how this works, but I guess it should be something like <a href="http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=/rzajw/rzajwdnsrr.htm" rel="noreferrer">this</a>.
  <hr>
  <strong>Added 3:</strong> OK, I see that one solution is to use <code>auto_prepend</code> option to insert a call to <code>session_set_save_handler()</code> in every script and write my own DB persister, perhaps throwing in calls to <code>memcached</code> for better performance. Fair enough.</p>
  
  <p>Is there also some way that I could avoid coding all this myself? Like some famous and well-tested PHP plugin?</p>
  
  <p><strong>Added much, much later:</strong> This is the way I went in the end: <a href="https://stackoverflow.com/questions/1022416/how-to-properly-implement-a-custom-session-persister-in-php-mysql">How to properly implement a custom session persister in PHP + MySQL?</a></p>
  
  <p>Also, I simply included the session handler manually in all pages.</p>