Using Memcache to implement synchronization session _php technique in Web cluster

Source: Internet
Author: User

Using Memcache to synchronize the session is still good, of course, you can also save the session through Redis, PHP can be opened and the session stored to the Redis cache, The following is the implementation process for setting up synchronization session sessions in a Web cluster using memcache:

1. Analog Web Cluster

I started two memcached processes to simulate two servers, respectively.

/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"

Description: The first line, session storage mode is Memcache, the second line, Memcache hash algorithm is consistent, the third line, 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), preparation of documents session.php

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

b), displaying session content Files

<?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 ID
echo $val; 
? >

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.