Turn Memcache save PHP session to solve multi-server session related issues

Source: Internet
Author: User
Tags php session
Turn Memcache save PHP session to solve multi-server session problem

?

?

PECL:: Package:: Memcache 2.1. There is one in version 1 of Changelog:


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

That is, you can use Memcache directly to make PHP Session.save_handler.


1. Install the memcached, or the set./configure; Make Make install, note memcached use Libevent for event-driven, so install libevent first.


2. Install Pecl::memcache, install with pecl command line tool:


PECL Install Memcache

Or install directly from the source:

Phpize

./configure

Make

Make install

Open php.ini extension=memcache.so, reboot Apache, see Phpinfo registered save handlers there will be files user memcache these 3 available.


3. Fix the configuration file and set the global settings in php.ini:


Session.save_handler = Memcache

Session.save_path = tcp://127.0.0.1:11211

Or a. htaccess in a directory:

Php_value Session.save_handler Memcache

Php_value Session.save_path tcp://127.0.0.1:11211

Or in one application:

Ini_set (Session.save_handler, memcache);

Ini_set (Session.save_path, tcp://127.0.0.1:11211);?

When multiple memcached servers are applied, they are separated by commas and, as stated in the Memcache::addserver () document, can take additional parameters persistent, weight, timeout, retry_interval, and so on , similar to this: 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 to the session storage in the program, test it with an example:


___fckpd___6

6. Check with SessionID to memcached:


$memcache = Memcache_connect (localhost, 11211);

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

SESSION[TEST3] = time ();

Print

;

Print session_id ();

?>

will have to see

String (PNS) test|i:1177556731; test3|i:1177556881;

This output proves that the session is working properly.


Using Memcache to store the session will be much faster than the files when reading and writing, and it will be more convenient when multiple servers need to share the session, and these servers will be configured to apply the same set of memcached servers, reducing the additional workload. The disadvantage is that the session data are stored in memory, the persistence of the lack of, but the session data is not a big problem.

  • 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.