PHP Load Balancer Session Shared _php Tutorial

Source: Internet
Author: User
It's been a long time since I've been working on the fourth edition of the Taiwan Lottery, so it's been a while since I've been writing something and sharing it with you today.

First of all, I believe we all know that the load balance can be very good to solve the problem of large traffic network, load balance is to the user's request to the multi-state PC to go to the processor, Since the division has been made, please consider the process of dealing with the session. In the case of PHP, the session will be stored in the temporary file of the machine, but in this case the session is lost, the simplest solution is to use memcache to entrust, A single set of services for the Memcache service (the best is a big one), the next job to the PHP code to do it, I do not want to modify the php.ini, because every change is too tired, and I am more lazy

Or maybe you'll tell me that directly modifying the php.ini session storage location can be written into the memcache, yes, no wrong, but this way does not adjust the GC release of the session, and today we have to say not only to let him, but also let him release the conversation.

Since to do, it is necessary to achieve perfection, the next side is my code, do not know how to read the explanation:

$_env=array (' SYS ' =>array (' memip ' = ' 127.0.0.1 ',//memcache's IP ' mempt ' =>11211,//memcache ' port ' memtim ' = >10,//memcache);//The session to Memcache, if you open the failure, it is said that Memcache is not configured to the final class s{public static function open () {/ /disable Session Auto-start session_write_close (); Ini_set (' Session.auto_start ', 0);//use one-fiftieth probability to activate GC cleanup session ini_set (' Session.gc_ Probability ', 1); Ini_set (' session.gc_divisor '); Ini_set (' session.use_cookies ', 1);//session's Life//ini_set (' Session.gc_maxlifetime ', $_env[' sys ' [' memtim ']);//ini_set (' Session.cookie_lifetime ', $_env[' sys ' [' memtim ']);// Ini_set (' Session.save_handler ', ' files '),//ini_set (' Session.save_path ', Run.        _tmp ');//ini_set (' Session.save_handler ', ' memcache ');//ini_set (' Session.save_path ', ' tcp://127.0.0.1:11211 '); $_env[' S_tim ' = Ini_get (' session.gc_maxlifetime ');//Establish Memcache-like $_env[' S_mem ']=new memcache;$_env[' s_mem ']-        >connect ($_env[' sys '] [' MEMIP '],$_env[' sys ' [' mempt ']) or Die (' Memcache connection failed! ');    return TRUE; }/** Read back: Read content/**/public static FuNction Read ($id) {return $_env[' S_mem ']->get (' S_ '. $id); }/** write-In return: bool/**/public static function write ($id, $data) {return $_env[' S_mem ']->set (' S_ '. $id, $data, MEMCACHE    _compressed,$_env[' SYS ' [' Memtim ']); }/** closed return: bool/**/public static function close () {$_env[' S_mem ']->close (); unset ($_env[' s_mem '],$_env[' SYS '] [' Memtim ']); return TRUE;}    /** Delete return: bool/**/public static function Destroy ($ID) {return $_env[' S_mem ']->delete (' S_ '. $id); }/** cleanup return: bool/**/public static function gc () {return TRUE;}} Session_set_save_handler (' S::open ', ' s::close ', ' s::read ', ' s::write ', ' S::d Estroy ', ' s::gc '); $_env[' SessionID ']= ( Isset ($_request[' SessionID '))? Trim ($_request["SessionID"]): session_id (); if ($_env[' SessionID ']!= ') {session_id ($_env[' SessionID ']);} Session_Start ();

http://www.bkjia.com/PHPjc/738545.html www.bkjia.com true http://www.bkjia.com/PHPjc/738545.html techarticle I've been busy with the fourth edition of the Taiwan Sports Lottery, so it's been a long time since I've written something and I'm going to share it with you today. First of all, to say the load balance, I believe everyone ...

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