Session sharing in PHP zookeeper balanced

Source: Internet
Author: User

Recently, I was busy developing a program for the fourth edition of the lottery coupons. So I haven't been on the trial for a long time. Today I will share some ideas with you.

First, let's talk about zookeeper balancing. I believe everyone knows that zookeeper balancing can solve the problem of high traffic on the website, zookeeper balancer is used to process the requests sent by users on multiple PCs. Since the requests are sent by users, so the session should be tested for handling methods. in other cases, php stores the session in the memory of the current machine. However, after the session is split, the session is lost, in fact, the most simple solution is to use memcache for render management. A single server is used as a memcache server (internal storage is better than one level ), the next job will be handed over to the PHP representative for help. I don't want to modify php here. ini, because every change is too tired, and I'm better than others.

Maybe you will tell me to modify php directly. the session storage location of ini can be merged into memcache. Yes, no problem. However, in this way, gc cannot be used to release the session in the past period, what we want to talk about today is not only to let him worry, but also to let him talk about it.

Now that we have to do this, we have to be perfect. The next generation is my generation, and I don't know how to look at it myself:

$ _ ENV = array ('sys '=> array ('memip' => '2017. 0.0.1 ', // MEMCACHE ip address 'mempt' => 11211, // MEMCACHE port 'memtime' => 10, // MEMCACHE timeout )); // render the SESSION to MEMCACHE. If the error message is displayed, it indicates that the final class S {public static function open () is not correctly configured in MEMCACHE () {// disable the session from initiating the specified session_write_close (); ini_set ('session. auto_start ', 0); // use the probability of 1/50 to perform automatic gc cleaning. The ini_set ('session. gc_probability ', 1); ini_set ('session. gc_divisor ', 50); ini_set ('session. use_cookies ', 1); // Session life // ini_set ('session. gc_maxlifetime', $ _ ENV ['sys '] ['memtime']); // ini_set ('session. cookie_lifetime ', $ _ ENV ['sys'] ['memtime']); // ini_set ('session. save_handler ', 'files'); // ini_set ('session. save_path ', Run. '_ tmp'); // ini_set ('session. save_handler ', 'memcac'); // ini_set ('session. save_path ', 'tcp: // 127.0.0.1: 11211'); // $ _ ENV ['s _ tim'] = ini_get ('session. gc_maxlifetime'); // creates a memcache object $ _ ENV ['s _ mem '] = n Ew Memcache; $ _ ENV ['s _ mem ']-> connect ($ _ ENV ['sys'] ['memip'], $ _ ENV ['sys '] ['mempt']) or die ('memcache failed! '); Return TRUE;}/** fetch and return: volume to content/**/public static function read ($ id) {return $ _ ENV ['s _ mem ']-> get ('s _'. $ id);}/** returns bool/**/public static function write ($ id, $ data) {return $ _ ENV ['s _ mem ']-> set ('s _'. $ id, $ data, MEMCACHE_COMPRESSED, $ _ ENV ['sys'] ['memtim']);}/** return: bool/**/public static function close () {$ _ ENV ['s _ mem ']-> close (); unset ($ _ ENV ['s _ mem '], $ _ ENV ['sys'] ['memtim']); return TRUE;}/** returns in addition: bool /**/ Public static function destroy ($ id) {return $ _ ENV ['s _ mem ']-> delete ('s _'. $ id) ;}/ ** return result: bool/**/public static function gc () {return TRUE ;}} session_set_save_handler ('s: open ','s:: close ','s: read','s: write','s: destroy','s: gc '); $ _ ENV ['sessionid'] = (isset ($ _ REQUEST ['sessionid'])? Trim ($ _ REQUEST ["sessionid"]): session_id (); if ($ _ ENV ['sessionid']! = '') {Session_id ($ _ ENV ['sessionid']);} session_start ();

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.