Using Memcache to synchronize the session is still good, of course, you can also save the session through Redis, PHP can be opened and the session stored to the Redis cache, The following is the implementation process for setting up synchronization session sessions in a Web cluster using memcache:
1. Analog Web Cluster
I started two memcached processes to simulate two servers, respectively.
/usr/local/bin/memcached-d-M 20-u zhangy-p 12000-p./memcached.pid
/usr/local/bin/memcached-d-M 20-u zhangy-p 13000-p./mem.pid
2, modify the configuration of PHP
Vi/usr/local/php/lib/php.ini
Session.save_handler = "Memcache"
Memcache.hash_strategy = "consistent"
Session.save_path = "tcp://127.0.0.1:13000?weight=10,tcp://127.0.0.1:12000"
Description: The first line, session storage mode is Memcache, the second line, Memcache hash algorithm is consistent, the third line, session storage status;
3, restart Apache
View Phpinfo
Session
| Session Support |
Enabled |
| Registered Save handlers |
Files User SQLite Memcache |
| Registered serializer Handlers |
PHP php_binary |
Immediately following are:
| Session.save_path |
tcp://127.0.0.1:13000,tcp://127.0.0.1:12000 |
tcp://127.0.0.1:13000,tcp://127.0.0.1:12000 |
4, do a simple test as follows:
A), preparation of documents session.php
<?php
session_start ();
$_session[' username ' = "abcabc";
Echo session_id ();
? >
b), displaying session content Files
<?php
$mem = new Memcache;
$mem->addserver ("127.0.0.1", 12000) or die ("could not add server 12000");
$mem->addserver ("127.0.0.1", 13000) or die ("could not add server 13000");
$val = $mem->get (' Qp0mrob2ovcqle3u4lbr4obsa5 ');
Echo session_id (); The resulting session ID
echo $val;
? >