PHP set Web cluster session synchronization steps

Source: Internet
Author: User
This time to bring you the PHP set Web cluster session synchronization steps in detail, PHP set Web cluster session synchronization of the note what, the following is the actual case, together to see.

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>

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

PHP+MONGODB database Operation steps

PHP closed case analysis of the use of practical cases

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.