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

PHP uses memcache to store session method summary

WBOY
WBOYOriginal
2016-07-29 09:06:44788browse

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)

php使用 memcache 来存储 session 方法总结

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

php使用 memcache 来存储 session 方法总结

Use sessionid to check in memcached:

?

1

2

3

4

5

6

<?php

$memcache= memcache_connect('localhost', 11211);

var_dump($memcache->get('19216821213c65 cedec65b0883238c278eeb573e077'));

$ memcache->set('aaaa', 'hello everyone');

var_dump($memcache->get( 'aaaa' ));

?> sion normal Work.

The above introduces the summary of PHP's use of memcache to store session methods, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.
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