This article mainly introduced the PHP implementation of distributed Memcache set Web Cluster session synchronization method, combined with the example of PHP set up and use Memcache implementation of the Web cluster session synchronization of the relevant operational skills and considerations, the need for friends can refer to the next
In this paper, we describe the method of PHP to implement distributed memcache to set up the session synchronization of Web cluster.
The default session for PHP is file storage:
Session.save_handler = Filessession.save_path = "/var/lib/php/session"
As a Web cluster, when session synchronization is required, it is a good idea to save the session to a distributed memcache to achieve shared synchronization.
Method:
The 1th type:
Vi/etc/php.ini
Session.save_handler = Memcachesession.save_path = "tcp://192.168.20.193:11211,tcp://192.168.20.194:11211"
The 2nd type:
Configured in PHP files using the Ini_set function for virtual hosts that cannot modify configuration files
<?php....ini_set ("Session.save_handler", "memcache"); Ini_set ("Session.save_path", "tcp://192.168.20.193:11211 , tcp://192.168.20.194:11211 "); >
Restarting the Web server
Service httpd Restart
View Phpinfo
Test
The login.php in 192.168.20.193
<?phpsession_start (); $_session[' login_time '] = time (); $_session[' username '] = ' test2 '; $token =session_id (); echo $ Token;//memache implementation $mem = new Memcache (), $mem->addserver (' 192.168.20.193 ', 11211); $mem->addserver (' 192.168.20.194 ', 11211);/*//memached implements $MEM = new Memcached (); $servers = Array (' 192.168.20.193 ', 11211, '), array (' 192.168.20.194 ', 11211, 67)); $mem->addservers ($servers); */echo '
The user.php in 192.168.20.194
<?php$mem = new Memcache (), $mem->addserver (' 192.168.20.193 ', 11211), $mem->addserver (' 192.168.20.194 ', 11211); $token =$_get[' token '];//get the Tokenprint_r ($mem->get ($token)); ><p><a href= "http://192.168.20.193/user.php?token=<?php echo $token;? > "rel=" External nofollow "target=" _balnk "> Return to 193 site's personal hub </a></p>
Articles you may be interested in:
PHP implementation of large file cutting and merging function example explained
An example of a simple group word algorithm implemented by PHP
A complete example of the MONGODB database operation class implemented by PHP