PHP realizes session sharing function under load balancing

  • 2021-09-20 19:35:40
  • OfStack

In this paper, an example is given to describe the sharing function of session under load balancing by PHP. Share it for your reference, as follows:

Recently busy with the development of Taiwan Sports Lottery version 4 of the program, so it has been a long time to write things up, today casually write something to share with you.

First of all, let's talk about load balancing under 1, I believe everyone knows that load balancing can solve the problem of large traffic on websites. Load balancing is to distribute users' requests to polymorphic pc for processing, Now that the request has been distributed, session should consider how to handle it. Traditionally, php stores session in a local temporary archive. But in this case after the distribution of session is lost, in fact, the simplest solution is to use memcache to host, set up a single server to use as memcache server (the memory is better 1 point), the next work to PHP code to do, this I do not want to modify php. ini, because each change is too tired, and I am lazy

Maybe you will tell me that you can write memcache by directly modifying the session storage location of php. ini under 1. Yes, that's right, but this way you can't call gc to release expired sessions. What we are talking about today is not only to let him host, but also to let him release sessions automatically.

Since we want to do it, we must do it perfectly. The following is my code, and I don't know how to read the comments by myself:


$_ENV=array(
  'SYS'=>array(
    'Memip'=>'127.0.0.1',      //MEMCACHE Adj. ip
    'Mempt'=>11211,       //MEMCACHE Adj. port
    'Memtim'=>10,          //MEMCACHE Super of 
  )
);
// Trusteeship SESSION To MEMCACHE, If opening fails , Explain that MEMCACHE Not configured correctly 
final class S{
  public static function open(){
  // Prohibit session Automatic opening 
    session_write_close();
  ini_set('session.auto_start',0);
  // Use 510 Divide 1 Probability startup of gc Cleanup session 
  ini_set('session.gc_probability',1);
  ini_set('session.gc_divisor',50);
  ini_set('session.use_cookies',1);
  //session Life span of 
  //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 Object 
  $_ENV['S_mem']=new Memcache;
  $_ENV['S_mem']->connect($_ENV['SYS']['Memip'],$_ENV['SYS']['Mempt']) or die('Memcache Connecting loss !');
    return TRUE;
  }
  /**
     Read 
     Return : Read the capacity 
  /**/
  public static function read($id){
    return $_ENV['S_mem']->get('s_'.$id);
  }
  /**
     Write 
     Return :bool
  /**/
  public static function write($id,$data){
    return $_ENV['S_mem']->set('s_'.$id,$data,MEMCACHE_COMPRESSED,$_ENV['SYS']['Memtim']);
  }
  /**
     Shut down 
     Return :bool
  /**/
  public static function close(){
    $_ENV['S_mem']->close();
    unset($_ENV['S_mem'],$_ENV['SYS']['Memtim']);
    return TRUE;
  }
  /**
     Division 
     Return :bool
  /**/
  public static function destroy($id){
    return $_ENV['S_mem']->delete('s_'.$id);
  }
  /**
     Clean up 
     Return :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();

For more readers interested in PHP related content, please check the topics on this site: "Summary of php Cache Technology", "Encyclopedia of PHP Array (Array) Operation Skills", "Summary of php String (string) Usage", "Summary of PHP Error and Exception Handling Methods", "Introduction to php Object-Oriented Programming", "Introduction to php+mysql Database Operation" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: