Home >Backend Development >PHP Tutorial >PHP uses memcache to store session method summary
Set session to use memcache to store
Method I: Set globally in php.ini
session.save_handler = memcache
session.save_path = "tcp://127.0.0.1:11211"
Method II: A certain directory .htaccess under
php_value session.save_handler "memcache"
php_value session.save_path "tcp://127.0.0.1:11211"
Method III: Or in a certain application
1 ini_set("session.save_handler", "memcache"); 2 ini_set("session.save_path", "tcp://127.0.0.1:11211");
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", etc. , similar to 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
Php code collection code
ini_set("session.save_handler", "memcached"); // It is 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']; 10 print "<br><br>"; 11 print $_SESSION['TEST3']; 12 print "<br><br>"; 13 print session_id(); 14 ?>
Use sessionid to check in memcached:
?
1 2 3 4 5 6 |
|