Multi-Server Session Sharing with PHP + Memcache

       
   Often a complete system may run across multiple servers. If these servers need to share sessions, PHP’s default file-based session storage falls short. In such cases, we can consider using Memcache to handle session storage and retrieval.

Step 1: Set Up the Required Environment on the Server

We won’t go into detail about installing PHP and a web server, as that’s not the main topic. What you additionally need are the Memcached service and the php_memcache extension.

Using CentOS as an example (Note: The installation commands below depend on your server configuration and are not guaranteed to be universal; please adjust them according to your actual situation.)

Install memcached 

  1. $ yum install memcached  

Install the php_memcache extension

  1. $ yum install php-pecl-memcache  

Or

  1. $ pecl install memcache  

Note: After installing the extension, you need to restart your web server for the changes to take effect.

Step 2: Start the Memcached Service Process

  1. $ /usr/bin/memcached -p 12321 -m 384M -u nobody -d  

Step 3: Verify the PHP Extension Is Installed Correctly

1. Run `php -m` on the command line and check if `memcache` appears in the results.

2. Create a `phpinfo()` page and check if `memcache` appears under the `Registered save handlers` value in the session section.

Step 4: Test on Two Servers

Sample test code:

 

  1. //

<?php
ini_set('session.save_handler', 'memcache');
ini_set('session.cookie_domain','.sample.com');
ini_set('session.save_path','tcp://10.22.229.141:12321?persistent=1&weight=1&timeout=1&retry_interval=15');

session_start();
if (!isset($_SESSION['session_time'])) {   
    $_SESSION['session_time'] = time();
}
echo "session_time:".$_SESSION['session_time']."<br />";
echo "now_time:".time()."<br />";
echo "session_id:".session_id()."<br />";
?>
//

 Save the code above (remember to replace the domain and IP), place it in the web directory of both servers, open two browser tabs, and visit the two addresses one after the other. If the timestamp after `session_time:` and the `session_id` are the same on both pages, it’s working.

Step 5: Modify Existing Code to Switch PHP’s session.save_handler

Add the following code to all entry-point files that need to share sessions (it must be placed before the `session_start()` function)

  1. ini_set('session.save_handler''memcache');  
  2. ini_set('session.cookie_domain','.sample.com');  
  3. ini_set('session.save_path','tcp://10.22.229.141:12321?persistent=1&weight=1&timeout=1&retry_interval=15');  

 

Alternatively, you can directly modify the corresponding values for these three lines in `php.ini`, which avoids needing to change existing PHP code. Choose the approach that best fits your situation.

The first line specifies the session storage handler.

The second line specifies the cookie domain for the generated session ID, i.e., the cookie domain you want to share sessions across. Remember to replace this with your own domain.

The third line is the session save path. Here, it uses a TCP connection to the Memcached port. Remember to replace this with the IP of your server providing the Memcache service.

That’s it, you’re all done.

 

Note: Because the Memcache protocol does not require authentication, anyone can access the data stored in Memcache. You need to set up firewall rules to block unauthorized IP access, or use the `-l` parameter when starting the Memcached process to specify listening only on a local network IP.

Of course, there are many other ways to achieve multi-server session sharing,

such as:

1. Tokyo Tyrant, which follows the same principle as Memcache.

2. Storing sessions in a database (requires manually cleaning up expired sessions periodically).

3. Using NFS file sharing so that multiple web servers share the disk where session files are saved. http://imysql.cn/?q=node/202

http://blog.csdn.net/rainday0310/article/details/7506621
 

Leave a Comment

Your email address will not be published.