PHP Multiple Concurrent Sessions Per User

后端 未结 5 986
感动是毒
感动是毒 2021-01-04 04:19

I\'m working on a web app using PHP on Apache. $_SESSION variables are used quite a bit for the information that must persist across pages.

We need each user to be a

5条回答
  •  北海茫月
    2021-01-04 04:55

    Non-Atomic Concurrent Session Management access can be simulated with the following pseudo coded logic:

    function main(){
      $locker = new SessionLocking();
      /** read elements the $_SESSION "cached" copy. **/
      $var1 = $_SESSION['var1'];
      $var2 = $_SESSION['var2'];
      /** Pseudo Atomic Read **/
      $locker->lock(); //session is locked against concurrent access.
      $var3 = $_SESSION['var3'];
      $locker->unlock(); //session is committed to disk (or other) and can be accessed by another script.
      /** Psuedo Atomic Write **/
      $locker->lock(); //session is locked against concurrent access.
      $_SESSION['var4'] = "Some new value";
      $locker->unlock(); //session is committed to disk (or other) and can be accessed by another script
    }
    
    CLASS SessionLocking {
    
    private static $lockCounter=0;
    private static $isLoaded=false;
    
    function __constructor(){
      if (!self::$isLoaded) load();
    }
    
    private function load(){
     $this->lock();
     $this->unlock();
    }
    
    private function lock(){
      if ($lockCounter<1) try {session_start();} Catch(){}
      $lockCounter++; 
    }
    
    private function unlock(){
      if ($lockCount<1) return;
      $lockCounter--;
      if ($lockCounter<1) try {session_write_close();} Catch(){}
    }
    }
    

提交回复
热议问题