Transferred from: http://koda.iteye.com/blog/466667
memcache and PHP memcach extension installation see http://koda.iteye.com/blog/665761
set session with Memcache to store
method I: Global settings in php.ini
PHP.ini Code
- Session.save_handler = Memcache
- Session.save_path = "tcp://127.0.0.1:11211"
Method II: In a directory. htaccess
PHP.ini Code
- Php_value session.save_handler "Memcache"
- Php_value session.save_path "tcp://127.0.0.1:11211"
Method III: Again or in one application
PHP code
- Ini_set ("Session.save_handler", "Memcache");
- Ini_set ("Session.save_path", "tcp://127.0.0.1:11211");
Use multiple memcached servers separated by commas "," and, as described in the memcache::addserver () documentation, can take additional parameters "persistent", "weight", "timeout", "Retry_ Interval "And so on, like this:" Tcp://host1:port1?persistent=1&weight=2,tcp://host2:port2 ".
If the installed pecl is memcached (which relies on the extension of the libmemcached library), the configuration should be
PHP code
- Ini_set ("Session.save_handler", "memcached"); //Is memcached not memcache
- Ini_set ("Session.save_path", "127.0.0.1:11211"); //Do not tcp:[/b]
code example (the one that does not depend on the libmemcached library)
PHP code
- <?php
- Session_Start ();
- if (!isset ($_session[' TEST '])) {
- $_session [' TEST '] = time ();
- }
- $_session [' TEST3 '] = time ();
- Print $_session[' TEST '];
- Print "<br><br>";
- Print $_session[' TEST3 '];
- Print "<br><br>";
- Print session_id ();
- ?>
use SessionID to go to memcached to check:
PHP code
- <?php
- $memcache = memcache_connect (' localhost ', 11211);
- Var_dump ($memcache->get (' 19216821213c65cedec65b0883238c278eeb573e077 '));
- $memcache->set (' aaaa ', ' Hello everyone ');
- Var_dump ($memcache->get (' aaaa '));
- ?>
'll see
String (PNS) "test|i:1177556731; test3|i:1177556881; "
This output proves that the session is working properly.
Resources:
Http://www.jb51.net/article/39073.htm
http://www.baidu.com/#wd =php%20session%20memcache&rsv_spt=1&issp=1&rsv_bp=0&ie=utf-8&tn= baiduhome_pg&rsv_sug3=8&rsv_sug4=466&rsv_sug1=8&oq=php%20session&rsv_sug2=1&f=3& rsp=8&inputt=7058
http://my.oschina.net/miaoyushun/blog/55180
http://imysql.cn/node/215
http://www.ratedir.com/news/420