1, directly modify the php.ini configuration file
Copy Code code as follows:
Session.save_handler = memcache//Set session storage mode to Memcache
Memcache.hash_strategy = "consistent"//Set hash algorithm for Memcache
Session.save_path = "tcp://127.0.0.100:11211"//Set the position of the session store, multiple memcache separated by commas, for example: tcp://127.0.0.1:11211,tcp:// 127.0.0.1:12000
2, using the directory of the. htaccess file Configuration
Copy Code code as follows:
Php_value Session.save_handler "Memcache"
Php_value Session.save_path "tcp://127.0.0.1:11211"
Description: This is only for Apache, the current use of nginx more, also do not recommend this approach.
3, in the project's PHP file to modify the configuration
Copy Code code as follows:
Ini_set ("Session.save_handler", "memcache");
Ini_set ("Session.save_path", "tcp://127.0.0.100:11211");
4. Test example
Copy Code code as follows:
Test session Read is normal
Session_Start ();
$_session[' username ' = "jb51.net";
Echo session_id ();
Read session from Memcache
$m = new Memcache ();
$m->connect (' localhost ', 11211);
Or so
$mem->addserver ("127.0.0.1", 11211) or Die ("Can ' t add Memcache server 127.0.0.1:12000");
Get data from session_id
This machine
$session = $m->get (session_id ()); session_id:d527b6f983bd5e941f9fff318a31206b
Another server, known session ID
$session = $m->get ("d527b6f983bd5e941f9fff318a31206b");
echo $session. " <br/> "; Will get this data: username|s:16: "Pandao";, parse it to get the corresponding value.
Echo session_id (). <br/> ";
Exit
Above just a few simple instructions, in fact, said Han off, generally speaking, do distributed, that must have server permissions, so recommend the first.