PHP uses Memcache to store session method summary

Source: Internet
Author: User

Set session with Memcache to store

Method I: Global settings in php.ini
Session.save_handler = Memcache
Session.save_path = "tcp://127.0.0.1:11211"


Method II: In a directory. htaccess
Php_value Session.save_handler "Memcache"
Php_value Session.save_path "tcp://127.0.0.1:11211"


Method III: Again or in one application

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 Collection Code
Ini_set ("Session.save_handler", "memcached"); It's memcached, not memcache.
Ini_set ("Session.save_path", "127.0.0.1:11211"); Don't tcp:[/b]


code example (the one that does not depend on the libmemcached library)

1 <?php  2 session_start ();  3 if (!isset ($_session[' test ')) {  4 $_session[' test '] = time ();  5}  6  7 $_session[' TEST3 ' = Time ();  8  9 Print $_session[' TEST '; print "<br><br>"; print $_session[' TEST3 "; print" <br><br > "; Print session_id (); ?>

Use SessionID to go to memcached to check:

?
123456 <?php $memcache = memcache_connect ( ' localhost ' 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.

PHP uses Memcache to store session method summary

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.