Set the session in PHP to use Memcache to store the method to summarize the _php instance

Source: Internet
Author: User

Memcached provides a custom session processor that can be used to store user session data to the memcached server. A completely separate memcached instance will be used internally, so you can set up a different server pool if you want. The key of the session is stored under the prefix Memc.sess.key, so if you use the same server pool for the session and the usual cache, be aware of this. Another session and usually the reason for the cache separation is that when the usual cache is full of the memcached server, it may cause your sessions to be kicked out of the cache, causing the user to fall out of line.

Session.save_handler string

Set to memcached to turn on the memcached session processor.

Session.save_path string

Defines a comma-delimited Hostname:port-style session cache server pool, for example: "sess1:11211, sess2:11211".

Method I: Global settings in php.ini

Session.save_handler = Memcache 

Method II: The. htaccess in a directory

Php_value Session.save_handler "Memcache" 

Method III: Again or in one application

Ini_set ("Session.save_handler", "memcache"); 

The use of multiple memcached servers is separated by commas "," and as described in the Memcache::addserver () document, with additional parameters such as "persistent", "weight", "timeout", "Retry_" Interval "And so on, like this:" Tcp://host1:port1?persistent=1&weight=2,tcp://host2:port2. "

If the installed pecl is memcached (which relies on the extension of the libmemcached library), the configuration should be

Ini_set ("Session.save_handler", "memcached"); is memcached not memcache 
ini_set ("Session.save_path", "127.0.0.1:11211");/Don't tcp:[/b]

code example (that doesn't depend on the libmemcached library)

<?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 (); 
? > 

Use SessionID to memcached query:

<?php 
$memcache = memcache_connect (' localhost ',); 
Var_dump ($memcache->get (' Ccedecbceebe ')); 
$memcache->set (' aaaa ', ' hello everyone '); 
Var_dump ($memcache->get (' aaaa ')); 
? >

'll see

String (Panax) "test|i:1177556731; test3|i:1177556881; "

This output proves that the session is working properly.

The following two usages to introduce you to the next PHP using memcached to store the session

One

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

Two

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.