{"id":2031,"date":"2010-08-02T08:49:58","date_gmt":"2010-08-02T07:49:58","guid":{"rendered":"http:\/\/www.bitvolution.com\/?p=881"},"modified":"2010-08-02T08:49:58","modified_gmt":"2010-08-02T07:49:58","slug":"session-sharing-in-php-the-easy-way","status":"publish","type":"post","link":"https:\/\/www.tomfotherby.com\/blog\/index.php\/2010\/08\/session-sharing-in-php-the-easy-way\/","title":{"rendered":"Session sharing in PHP &#8211; The easy way"},"content":{"rendered":"<p><a href=\"http:\/\/www.php.net\/\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.tomfotherby.com\/blog\/wp-content\/uploads\/2010\/08\/logoPHP.png?resize=100%2C53&#038;ssl=1\" alt=\"\" width=\"100\" height=\"53\" class=\"alignleft size-full wp-image-896\" \/><\/a><\/p>\n<p>I used to think that a hard part of scaling a website from one webserver to two webservers was sharing the sessions between the machines to keep the users logged in whichever machine they were served by. I was pleasantly surprized that it is possible to accomplish sharing sessions between two servers by changing only 5 lines in the <em>php.ini<\/em> file (the 2 biggies are <code>session.save_handler<\/code> and <code>session.save_path<\/code>).<\/p>\n<p>Here is a solution for <a href=\"http:\/\/www.redhat.com\/rhel\/\">RHEL 5.5<\/a>:<\/p>\n<p><!--more--><\/p>\n<ol>\n<li>Install the <a href=\"http:\/\/memcached.org\/\">memcached<\/a> server &#8211; <em>a distributed memory object caching system<\/em>:\n<ul>\n<li><em>libmemcached<\/em> is unfortunately not part of the RHEL standard packages so we have to install a third party repository first following instructions at <a href=\"http:\/\/blog.famillecollet.com\/pages\/Config-en\">http:\/\/blog.famillecollet.com\/pages\/Config-en<\/a>.<\/li>\n<li>After installing the 3rd party repo, install <em>libmemcached<\/em>: <span class=\"iconTerminal\">sudo yum &#8211;enablerepo=epel &#8211;enablerepo=remi update memcached &amp;&amp; sudo yum &#8211;enablerepo=epel &#8211;enablerepo=remi install libmemcached libmemcached-devel<\/span><\/li>\n<\/ul>\n<\/li>\n<li>Install the PHP memcache client: <span class=\"iconTerminal\">sudo pecl install memcache-3.0.3<\/span>\n<ul>\n<li>The memcache PECL extension includes a session handler which gives us a alternative option to the default PHP session handler (which uses files). Be sure to answer &#8220;yes&#8221; when the installer asks to &#8220;<em>Enable memcache session handler support?<\/em>&#8220;.<\/li>\n<\/ul>\n<\/li>\n<\/li>\n<li>Modify the <em>php.ini<\/em> file of both servers (e.g. <em>\/etc\/php.ini<\/em>) so that the PHP memcache client is used instead of session files:<br \/>\n<code><s>session.save_handler = files<\/s><br \/>\nsession.save_handler = memcache<\/code>\n<\/li>\n<li>Modify the PHP <em>memcache.ini<\/em> file of both servers (e.g. <em>\/etc\/php.d\/memcache.ini<\/em>) so that the PHP memcache client is configured to share sessions with redundancy and failover support:<br \/>\n<code>memcache.hash_strategy = consistent<br \/>\nmemcache.allow_failover = 1<br \/>\nmemcache.session_redundancy=2<br \/>\nsession.save_path=\"tcp:\/\/192.168.0.1:11211, tcp:\/\/192.168.0.2:11211\"<\/code>\n<\/li>\n<\/ol>\n<p>This approach shares sessions between both machines using RAM as storage. The PHP memcache client cleverly writes the session data to both machines so that if one machine fails all users will still be logged in when they are served by the remaining machine.<\/p>\n<p>My exploration of this subject is documented on serverfault: <a href=\"http:\/\/serverfault.com\/questions\/164350\/can-a-pool-of-memcache-daemons-be-used-to-share-sessions-more-efficiently\">can-a-pool-of-memcache-daemons-be-used-to-share-sessions-more-efficiently<\/a>. My conclusion is that this method of session sharing is not the best approach but it is the simplest to set up.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I used to think that a hard part of scaling a website from one webserver to two webservers was sharing the sessions between the machines to keep the users logged in whichever machine they were served by. I was pleasantly surprized that it is possible to accomplish sharing sessions between two servers by changing only [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[79],"tags":[100],"class_list":["post-2031","post","type-post","status-publish","format-standard","hentry","category-technical","tag-php"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.tomfotherby.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2031","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tomfotherby.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tomfotherby.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tomfotherby.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tomfotherby.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=2031"}],"version-history":[{"count":0,"href":"https:\/\/www.tomfotherby.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2031\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tomfotherby.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=2031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tomfotherby.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=2031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tomfotherby.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=2031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}