Home >Backend Development >PHP Tutorial >PHP uses memcache to store session method summary, memcachesession_PHP tutorial
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: .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 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" " 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
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 |
<?php
$memcache = memcache_connect( 'localhost' , 11211);
var_dump( $memcache ->get( '19216821213c65cedec65b0883238c278eeb573e077' ));
$memcache ->set( 'aaaa' , 'hello everyone' );
var_dump( $memcache ->get( 'aaaa' ));
?>
|
You will see
string(37) "TEST|i:1177556731;TEST3|i:1177556881;"
Output like this proves that the session is working normally.