Home >Backend Development >PHP Tutorial >Summary of methods for setting up sessions to be stored using memcache in PHP, sessionmemcache_PHP tutorial

Summary of methods for setting up sessions to be stored using memcache in PHP, sessionmemcache_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:00:47897browse

A summary of how to set up session storage using memcache in PHP. sessionmemcache

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)

<&#63;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(); 
&#63;> 

Use sessionid to query in memcached:

<&#63;php 
$memcache = memcache_connect('localhost', ); 
var_dump($memcache->get('ccedecbceebe')); 
$memcache->set('aaaa', 'hello everyone'); 
var_dump($memcache->get('aaaa')); 
&#63;>

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");

Articles you may be interested in:

  • Using Memcached to implement session mechanism under PHP to replace PHP’s native session support
  • Detailed explanation of using memcache to store sessions based on PHP
  • How to set up session in PHP into memcached
  • Three configuration methods for using memcache to store session in PHP

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1091855.htmlTechArticleA summary of the method of setting up session in php and using memcache to store it, sessionmemcache memcached provides a custom session processor Can be used to store user session data to the memcached server...
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