memcached provides a custom session processor that can be used to store user session data into the memcached service. end. A completely separate memcached instance will be used internally, so you can set up a different server pool if needed. Session keys are stored under the prefix memc.sess.key., so please be aware of this if you are using the same server pool for sessions and normal caching. Annotation: Another reason why the session is separated from the normal cache is that when the normal cache fills up the memcached server, your session may be kicked out of the cache, causing the user to be disconnected inexplicably.
session.save_handler string
Set memcached to enable memcached's session processor.
session.save_path string
Define a comma-separated hostname:port style session cache server pool, for example: "sess1:11211, sess2:11211".
Method I: Set globally in php.ini
session.save_handler = memcache session.save_path = "tcp://127.0.0.1:11211"
Method II: .htaccess in a certain directory
php_value session.save_handler "memcache" php_value session.save_path "tcp://127.0.0.1:11211"
Method III: Or in an application
ini_set("session.save_handler", "memcache"); ini_set("session.save_path", "tcp://...:");
When using multiple memcached servers, separate them with commas ",", and as explained in the Memcache::addServer() document, you can take additional parameters "persistent", "weight", "timeout", "retry_interval" " Wait, something like this: "tcp://host1:port1?persistent=1&weight=2,tcp://host2:port2".
If the installed PECL is memcached (the extension that relies on the libmemcached library), the configuration should be
ini_set("session.save_handler", "memcached"); // 是memcached不是memcache ini_set("session.save_path", "127.0.0.1:11211"); // 不要tcp:[/b]
Code example (the one that does not depend on the libmemcached library)
<?php session_start(); if (!isset($_SESSION['TEST'])) { $_SESSION['TEST'] = time(); } $_SESSION['TEST'] = time(); print $_SESSION['TEST']; print "<br><br>"; print $_SESSION['TEST']; print "<br><br>"; print session_id(); ?>
Use sessionid to query in memcached:
<?php $memcache = memcache_connect('localhost', ); var_dump($memcache->get('ccedecbceebe')); $memcache->set('aaaa', 'hello everyone'); var_dump($memcache->get('aaaa')); ?>
will see
string(37) "TEST|i:1177556731;TEST3|i:1177556881;"
Output like this proves that the session is working normally.
The following uses two usage examples to introduce to you how to use memcached to store sessions in php
1.
ini_set("session.save_handler", "memcache"); ini_set("session.save_path","tcp://127.0.0.1:11211");
Multiple memcached
ini_set("session.save_path","tcp://127.0.0.1:11211,tcp://127.0.0.1:11211");
2.
ini_set("session.save_handler", "memcached"); ini_set("session.save_path","...:");
Multiple memcached
ini_set("session.save_path","127.0.0.1:11211,127.0.0.1:11211");