| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 
 | class SessionSerializer
 {
 public static function encode($array, $safe = true, $method = '')
 {
 $method = empty($method) ? ini_get("session.serialize_handler") : $method;
 switch ($method) {
 case "php":
 return self::serializePhp($array, $safe);
 break;
 case "php_binary":
 return self::serializePhpbinary($array, $safe);
 break;
 default:
 throw new Exception("Unsupported session.serialize_handler: " . $method . ". Supported: php, php_binary");
 }
 }
 
 public static function serializePhp($array, $safe = true)
 {
 if ($safe) {
 $array = unserialize(serialize($array));
 }
 $raw  = '';
 $line = 0;
 $keys = array_keys($array);
 foreach ($keys as $key) {
 $value = $array[$key];
 $line++;
 $raw .= $key . '|';
 if (is_array($value) && isset($value['huge_recursion_blocker_we_hope'])) {
 $raw .= 'R:' . $value['huge_recursion_blocker_we_hope'] . ';';
 } else {
 $raw .= serialize($value);
 }
 $array[$key] = ['huge_recursion_blocker_we_hope' => $line];
 }
 return $raw;
 }
 
 private static function serializePhpbinary($array, $safe = true)
 {
 return '';
 }
 
 public static function decode($session_data, $method = '')
 {
 $method = empty($method) ? ini_get("session.serialize_handler") : $method;
 switch ($method) {
 case "php":
 return self::unserializePhp($session_data);
 break;
 case "php_binary":
 return self::unserializePhpbinary($session_data);
 break;
 default:
 throw new Exception("Unsupported session.serialize_handler: " . $method . ". Supported: php, php_binary");
 }
 }
 
 private static function unserializePhp($session_data)
 {
 $return_data = [];
 $offset      = 0;
 while ($offset < strlen($session_data)) {
 if (!strstr(substr($session_data, $offset), "|")) {
 throw new Exception("Invalid data, remaining: " . substr($session_data, $offset));
 }
 $pos     = strpos($session_data, "|", $offset);
 $num     = $pos - $offset;
 $varname = substr($session_data, $offset, $num);
 $offset += $num + 1;
 $data                  = unserialize(substr($session_data, $offset));
 $return_data[$varname] = $data;
 $offset += strlen(serialize($data));
 }
 return $return_data;
 }
 
 private static function unserializePhpbinary($session_data)
 {
 $return_data = [];
 $offset      = 0;
 while ($offset < strlen($session_data)) {
 $num = ord($session_data[$offset]);
 $offset += 1;
 $varname = substr($session_data, $offset, $num);
 $offset += $num;
 $data   = unserialize(substr($session_data, $offset));
 $return_data[$varname] = $data;
 $offset += strlen(serialize($data));
 }
 return $return_data;
 }
 }
 
 
 class SessionRedis
 {
 protected $lifeTime     = 900;
 
 protected $sessionName  = 'your_laravel_app_name:';
 
 protected $cookieDomain = 'yourdomain.com';
 protected $handle       = null;
 
 public function open($savePath, $sessName)
 {
 $this->handle = new Redis;
 $this->handle->connect('127.0.0.1', 6379);
 return true;
 }
 
 public function close()
 {
 $this->gc(ini_get('session.gc_maxlifetime'));
 $this->handle->close();
 $this->handle = null;
 return true;
 }
 
 public function read($sessID)
 {
 if (empty($sessID)) {
 return;
 }
 
 $sessData = $this->handle->get($this->sessionName . $sessID);
 
 $sessData = unserialize(unserialize($sessData));
 
 $sessData = is_array($sessData) ? $sessData : [];
 
 $sessData = SessionSerializer::encode($sessData);
 
 return $sessData;
 }
 
 public function write($sessID, $sessData)
 {
 if (empty($sessID)) {
 return;
 }
 
 $sessData = SessionSerializer::decode($sessData);
 
 $sessData = serialize(serialize($sessData));
 
 return $this->handle->setex($this->sessionName . $sessID, $this->lifeTime, $sessData);
 }
 
 public function destroy($sessID)
 {
 if (empty($sessID)) {
 return;
 }
 
 return $this->handle->delete($this->sessionName . $sessID);
 }
 
 public function gc($sessMaxLifeTime)
 {
 return true;
 }
 
 public function start()
 {
 ini_set('session.cookie_domain', isset($m[0]) ? $m[0] : $this->cookieDomain);
 ini_set('session.auto_start', 0);
 ini_set('session.use_trans_sid', 0);
 ini_set('session.gc_probability', 1);
 ini_set('session.gc_divisor', 1000);
 ini_set('session.gc_maxlifetime', $this->lifeTime);
 ini_set('session.use_cookies', 1);
 ini_set('session.cookie_path', '/');
 session_module_name('user');
 session_set_save_handler(
 array(&$this, 'open'),
 array(&$this, 'close'),
 array(&$this, 'read'),
 array(&$this, 'write'),
 array(&$this, 'destroy'),
 array(&$this, 'gc')
 );
 session_start();
 }
 }
 
 (new SessionRedis())->start();
 
 |