Session. save_handler of memcache PHP.

Source: Internet
Author: User

You can directly use memcache for PHP session. save_handler.

1. Install memcached, or the set./configure; Make; make install. Note that memcached uses libevent as the event driver, so install libevent first.

2. Install PECL: memcache and install it using the PECL command line tool:

pecl install memcache

Or install it directly from the source code:

phpize
./configure
make
make install

Open extension = memcache. So in PHP. ini and restart Apache. Check that "registered save handlers" in phpinfo has the "files user memcache" available.

3. modify the configuration file and set it globally in PHP. ini:

session.save_handler = memcache
session.save_path = "tcp://127.0.0.1:11211"

Or. htaccess in a directory:

php_value session.save_handler "memcache"
php_value session.save_path "tcp://127.0.0.1:11211"

Or in an application:

ini_set("session.save_handler", "memcache");
ini_set("session.save_path", "tcp://127.0.0.1:11211");

Multiple memcached servers are separated by commas (,), which are the same as those described in the memcache: addserver () document, you can include additional parameters such as "persistent", "weight", "timeout", and "retry_interval". For example, "TCP: // host1: port1? Persistent = 1 & Weight = 2, TCP: // host2: port2 ".

4. Start memcached:

memcached -d -l 127.0.0.1 -p 11212 -m 128

5. Use memcache in the program for session storage. Use an example to test it:

<?php
session_start();
if (!isset($_SESSION['TEST'])) {
$_SESSION['TEST'] = time();
}

$_SESSION['TEST3'] = time();

print $_SESSION['TEST'];
print "<br><br>";
print $_SESSION['TEST3'];
print "<br><br>";
print session_id();
?>

6. Use sessionid to query in memcached:

<?php
$memcache = memcache_connect('localhost', 11211);
var_dump($memcache->get('19216821213c65cedec65b0883238c278eeb573e077'));
?>

You will see

string(37) "TEST|i:1177556731;TEST3|i:1177556881;"

This output proves that the session works normally.

Using memcache to store sessions is much faster in reading and writing than when using files, and it is more convenient when multiple servers need to share sessions, you can configure these servers to use the same group of memcached servers, reducing the additional workload. The disadvantage is that session data is stored in memory, which lacks persistence, but it is not a big problem for session data.

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.