[Turn]php use Memcache to store session

Source: Internet
Author: User
Tags vars

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
    1. Session.save_handler = Memcache
    2. Session.save_path = "tcp://127.0.0.1:11211"



Method II: In a directory. htaccess  

PHP.ini Code
    1. Php_value session.save_handler "Memcache"
    2. Php_value session.save_path "tcp://127.0.0.1:11211"



Method III: Again or in one application  

PHP code
    1. Ini_set ("Session.save_handler", "Memcache");
    2. 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
    1. Ini_set ("Session.save_handler", "memcached"); //Is memcached not memcache
    2. 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
  1. <?php
  2. Session_Start ();
  3. if (!isset ($_session[' TEST '])) {
  4. $_session [' TEST '] = time ();
  5. }
  6. $_session [' TEST3 '] = time ();
  7. Print $_session[' TEST '];
  8. Print "<br><br>";
  9. Print $_session[' TEST3 '];
  10. Print "<br><br>";
  11. Print session_id ();
  12. ?>



use SessionID to go to memcached to check:  

PHP code
    1. <?php
    2. $memcache = memcache_connect (' localhost ', 11211);
    3. Var_dump ($memcache->get (' 19216821213c65cedec65b0883238c278eeb573e077 '));
    4. $memcache->set (' aaaa ', ' Hello everyone ');
    5. Var_dump ($memcache->get (' aaaa '));
    6. ?>



'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

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.