Convert memcache to save php sessions to solve problems related to multi-server sessions

Source: Internet
Author: User
Tags php session
Transfer memcache to save php sessions to solve the problem of multi-server sessions ?? One of the Changelog versions of PECL: Package: memcache2.1.1 is-Addedexperiment-to-memcache-to save the php session to solve the multi-server session problem.

?

?

In PECL: Package: memcache 2.1.1, Changelog contains one of the following:


-Added experimental session storage support. You can use memcached as session storage.

That is, you can directly use memcache for PHP session. save_handler.


1. install memcached, or the set./configure; 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 three 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 (,). They are the same as those described in the Memcache: addServer () document. they can contain 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. apply memcache in the program for session storage. use an example to test it:


___ FCKpd ___ 6

6. use sessionid to query in memcached:


$ Memcache = memcache_connect (localhost, 11211 );

Var_dump ($ memcache-> get (19216821213c65cedec65b0883238c278eeb573e077 ));

SESSION [TEST3] = time ();

Print

;

Print session_id ();

?>

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 for multiple servers to share sessions, you can configure these servers to apply 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.