Home >Backend Development >PHP Tutorial >PHP uses memcache to store session method summary_PHP tutorial

PHP uses memcache to store session method summary_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-13 10:42:09906browse

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

<span 1</span> <span ini_set</span>("session.save_handler", "memcache"<span ); 
</span><span 2</span> <span ini_set</span>("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)

<span  1</span> <?<span php 
</span><span  2</span> <span session_start</span><span (); 
</span><span  3</span> <span if</span> (!<span isset</span>(<span $_SESSION</span>['TEST'<span ])) { 
</span><span  4</span> <span $_SESSION</span>['TEST'] = <span time</span><span (); 
</span><span  5</span> <span } 
</span><span  6</span> 
<span  7</span> <span $_SESSION</span>['TEST3'] = <span time</span><span (); 
</span><span  8</span> 
<span  9</span> <span print</span> <span $_SESSION</span>['TEST'<span ]; 
</span><span 10</span> <span print</span> "<br><br>"<span ; 
</span><span 11</span> <span print</span> <span $_SESSION</span>['TEST3'<span ]; 
</span><span 12</span> <span print</span> "<br><br>"<span ; 
</span><span 13</span> <span print</span> <span session_id</span><span (); 
</span><span 14</span> ?>




Use sessionid to check in memcached:

<?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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/667911.htmlTechArticleSet session to use memcache to store method I: Globally set session.save_handler = memcache session.save_path in php.ini = "tcp://127.0.0.1:11211" Method II: .h in a certain directory...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn