Laravel is compatible with native PHPsession.
- Author: Chen Liujin
- Home: http://www.chenliujin.com
- Email: liujin.chen@qq.com
Background
The laravel framework is used to reconstruct the zencart site or projects implemented by other frameworks (such as ThinkPHP) and migrate the business to the laravel project step by step. in this case, the sessions on both sides need to be shared. The native PHP session data generated by laravel can be read, and vice versa, which greatly reduces the complexity of system reconstruction.
Solution
Use the same serialization algorithm to ensure that the serialized results on both sides are consistent.
Laravel session serialization method
Serialize/unserialize
Session. serialize_handler
Session. serialize_handler defines the name of the processor used for serialization/deserialization. Currently, PHP Serialization formats (php_serialize), PHP internal formats (php and php_binary), and WDDX (wddx) are supported ). If WDDX support is added during PHP compilation, only WDDX can be used. Php_serialize can be used since PHP 5.5.4. Php_serialize simply uses the serialize/unserialize function internally without restrictions imposed by php and php_binary. Using an older serialization processor causes $ _ SESSION indexes to neither be digits nor contain special characters (| and !) . Use php_serialize to avoid errors caused by numbers and special character indexes when the script exits. Php is used by default.
Processor |
Storage format |
Php_binary |
ASCII characters corresponding to the length of the key name + values processed by the deserialization function of serialize () |
Php |
Key name + vertical bar + value processed by the deserialization function of serialize () |
Php_serialize (php> = 5.5.4) |
Array processed by the deserialization of the serialize () function |
Php 5.3.3
Php 5.5.32
PHP
Laravel session. name: config/session. php
'Cookies' => 'zenid', # consistent with zencart
Redis key prefix: config/cache. php
['Redis '=> ['driver' => 'redis ', 'connection' => 'default', 'prefix' => 'phpredis _ SESSION ', # consistent with zencart],],
Laravel session storage: 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 read zencart write and laravel read normally.
-
Zencart write
PHPREDIS_SESSION: 02103fd8mo5jb7qia51lnu7lh5
"a:1:{s:13:\"securityToken\";s:32:\"3c83dcba20e98cfd77ba70db6de93497\";}"
-
Laravel read
Reading failed
-
Debug
Handler-> read ($ this-> getId (); error_log (var_export ($ data, true); # trace the data retrieved from redis, 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
$ Data = $ this-> handler-> read ($ this-> getId (), $ data is an array, which confirms that deserialization is successful, however, handler-> read deserializes multiple times (a string should be obtained before deserialization), indicating that handler-> read has the deserialization function. Therefore, one more serialization and deserialization are required for zencart storage and reading.
Zencart performs one-time serialization and deserialization.
-
Write
Pconnect (SESSION_REDIS_HOST_NEW, SESSION_REDIS_PORT_NEW); $ redis_new-> auth (token); $ redis_new-> select (token); $ 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 (token); $ redis_new-> select (token); $ rd_ssk = 'phpredis _ SESSION :'. $ key; $ sess_value = $ redis_new-> get ($ rd_ssk); $ sess_value = unserialize ($ sess_value); # deserialization return $ sess_value ;}
- Redis
"s:67:\"a:1:{s:13:\"securityToken\";s:32:\"8a190ebc150a39dd8a7bd46a9c2665cc\";}\";"
- Laravel read successfully
Laravel write, zencart read
-
Laravel write
AddBagDataToSession (); $ this-> ageFlashData (); # simulate session assignment $ this-> attributes = array ('securitytoken' => '03eac95413cbfcc16ea599f36d2e24e2 ',); $ this-> handler-> write ($ this-> getId (), $ this-> prepareForStorage (serialize ($ this-> attributes ))); $ this-> started = false ;}
- Redis
- Zencart read
Session key conflict
Both sides save the language in the session. at this time, the key and value must be consistent. Otherwise, the data of the other side will be overwritten, causing an exception.