Laravel Compatible native PHP session

Source: Internet
Author: User
Tags pconnect

Copyright

    • Author: Chen Liujin
    • Home: http://www.chenliujin.com
    • E-mail: liujin.chen@qq.com

Background

Use the Laravel framework to refactor projects implemented by Zencart sites or other frameworks, such as thinkphp, to move the business step-by-step to the Laravel project, where both sides of the session need to be shared. Need to implement Laravel generated session data PHP native session can read, and vice versa, and ultimately greatly reduce the complexity of system refactoring.

Scheme

The same serialization algorithm is used to keep the results consistent on both sides of the serialization.

The method of Laravel session serialization

Serialize/unserialize

Session.serialize_handler

Session.serialize_handler defines the name of the processor used for serialization/deserialization. PHP serialization Format (named Php_serialize), PHP PHP internal format (named PHP and Php_binary) and WDDX (named WDDX) are currently supported. If PHP is compiled with WDDX support, you can only use WDDX. Php_serialize can be used from PHP 5.5.4. Php_serialize simply uses the Serialize/unserialize function internally, and there is no limit to PHP and php_binary. Using an older serialization processor causes the index of the $_session to be neither a number nor a special character (| and!). Using php_serialize to avoid script exits, numeric and special character indexes cause an error. PHP is used by default.

Processor the corresponding storage format
Php_binary The length of the key name corresponds to the ASCII character + key name + the value processed by the Serialize () function deserialization
Php Key name + vertical bar + serialize () function deserialization value
Php_serialize (php>=5.5.4) Arrays processed by serialize () function deserialization

PHP 5.3.3

PHP 5.5.32

Php

    • Upgrade PHP to 5.5
    • Ini
      [Session]session.serialize_handler = php_serialize # using the new serialization method

Laravel

session.name:config/session.php

    ' Cookie ' = ' Zenid ', # Consistent with Zencart

Redis Key prefix:config/cache.php

 
  [        ' Redis ' = [            ' driver ' = ' redis ',            ' connection ' = ' default ',            ' prefix ' and ' = ' phpredis_ SESSION ', # Consistent with Zencart         ],    ],

Laravel Session Store: illuminate\session\store.php

 
  Addbagdatatosession ();         $this->ageflashdata ();         $this->handler->write ($this->getid (), $this->prepareforstorage (Serialize ($this->attributes));         $this->started = false;    }

Laravel Session read: illuminate\session\store.php

 
  Handler->read ($this->getid ());         if ($data) {            $data = @unserialize ($this->prepareforunserialize ($data));             if ($data!== false && $data!== null && is_array ($data)) {                return $data;            }        }         return [];    }

Verify that the session data saved by the other party can be read normally

Zencart Write, Laravel read

  • Zencart Write

    Phpredis_session:02103fd8mo5jb7qia51lnu7lh5

    "A:1:{s:13:\" securitytoken\ "s:32:\" 3c83dcba20e98cfd77ba70db6de93497\ ";}"

  • Laravel Read

    Read failed

  • Debug

      
        Handler->read ($this->getid ());       Error_log (Var_export ($data, true)); # Track data taken from Redis to determine the cause of the problem        if ($data) {          $data = @unserialize ($this->prepareforunserialize ($data));           if ($data!== false && $data!== null && is_array ($data)) {              return $data;          }      }       return [];  }
  • Error_log

    [15-mar-2016 07:59:01 UTC] Array (' securitytoken ' = ' 03eac95413cbfcc16ea599f36d2e24e2 ',)
  • Cause of the problem

    $data = $this->handler->read ($this->getid ()), $data is an array that confirms that it can be deserialized successfully, but is Handler->read Multi-deserialization once (the fetch should be a string, followed by deserialization), indicating that Handler->read has deserialization capability. Therefore, multiple serialization and deserialization are required for zencart saving and reading.

Zencart to serialize and deserialize more than once

  • Write

      
        Pconnect (Session_redis_host_new, session_redis_port_new);  $redis _new->auth (session_redis_password_new);  $redis _new->select (session_redis_db_new);   $rd _ssk = ' phpredis_session: '. $key;  $redis _new->setex ($rd _ssk, $SESS _life, $val);  return true;}
  • Read

      
        Pconnect (Session_redis_host_new, session_redis_port_new);  $redis _new->auth (session_redis_password_new);  $redis _new->select (session_redis_db_new);  $rd _ssk = ' phpredis_session: '. $key;  $sess _value = $redis _new->get ($rd _ssk);   $sess _value = unserialize ($sess _value); #反序列化    return $sess _value;}
  • Redis
    "S:67:\" a:1:{s:13:\ "securitytoken\"; s:32:\ "8a190ebc150a39dd8a7bd46a9c2665cc\";} \";"
  • Laravel Read Success

Laravel Write, Zencart read

    • Laravel Write

         
          Addbagdatatosession ();   $this->ageflashdata ();   # Analog Session Assignment     $this->attributes = Array (      ' securitytoken ' = ' 03eac95413cbfcc16ea599f36d2e24e2 ',  ) ;   $this->handler->write ($this->getid (), $this->prepareforstorage (Serialize ($this->attributes));   $this->started = false;}
    • Redis
      • Key phpredis_session:6d8d36a7abe4318891c60308f47bb8ed489bfb5a
      • Value
        "S:67:\" a:1:{s:13:\ "securitytoken\"; s:32:\ "03eac95413cbfcc16ea599f36d2e24e2\";} \";"
      • The data for Laravel and Zencart writing to Redis remains consistent at this time
    • Zencart Read

Session key conflict

Both sides in the session to save language, the need to keep the key and the value definition consistent, otherwise it will overwrite the other's data, causing an exception.

    • Zencart
      $_session[' language '] = ' 中文版 ';
    • Laravel
      Session::p ut (' language ', ' en ');
  • Related Article

    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.