PHP implementation of distributed Memcache set the Web cluster session synchronization method detailed

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.