Synchronization session with Memcache in a Web cluster _php tutorial

Source: Internet
Author: User
It is good to use Memcache to synchronize sessions, of course, you can also save the session via Redis, which allows PHP to open and store the session in the Redis cache. The following is a procedure for setting up a session with Memcache in a Web cluster:

1. Simulating a Web cluster

I started two memcached processes, simulating two servers separately.

/usr/local/bin/memcached-d-M 20-u zhangy-p 12000-p./memcached.pid
/usr/local/bin/memcached-d-M 20-u zhangy-p 13000-p./mem.pid

2, modify the configuration of PHP

Vi/usr/local/php/lib/php.ini

Session.save_handler = "Memcache"
Memcache.hash_strategy = "consistent"
Session.save_path = "tcp://127.0.0.1:13000?weight=10,tcp://127.0.0.1:12000"

Note: The first line, the session storage method is memcache; the second line, Memcache hash algorithm is consistent; third row, session storage status;

3. Restart Apache

View Phpinfo

Session

Session Support Enabled
Registered Save handlers Files User SQLite Memcache
Registered serializer Handlers PHP php_binary

Immediately following are:

Session.save_path tcp://127.0.0.1:13000,tcp://127.0.0.1:12000 tcp://127.0.0.1:13000,tcp://127.0.0.1:12000

4, do a simple test as follows:

A), Prepare documents session.php

<?php session_start (); $_session[' username '] = "ABCABC"; Echo session_id ();?>

b), display session content file

<?php $mem = new Memcache; $mem->addserver ("127.0.0.1", 12000) or Die ("Could not add server 12000"); $mem->addserver ("127.0.0.1", 13000) or Die ("Could not add server 13000"); $val = $mem->get (' Qp0mrob2ovcqle3u4lbr4obsa5 '); Echo session_id (); The resulting session Idecho $val;?>

http://www.bkjia.com/PHPjc/824607.html www.bkjia.com true http://www.bkjia.com/PHPjc/824607.html techarticle using the Memcache to synchronize the session is still good, of course, can also be used to save the session via Redis, PHP can be opened and stored in the session to the Redis cache, the following is set to use Memcache in W ...

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