public static function getLoginContext() { $config = array('token' => '_user_', 'path' => '/', 'sign_salt' => function ($context) { do { if (!isset($context['id']) || !$context['id']) { break; } if (!($user = \Model\User::find($context['id']))) { break; } // 以用户密码作为salt return $user->passwd; } while (false); // 如果用户不存在,就用固定的随机字符串 return 'fdq0rj32jr0dsjfwf'; }); $handler = new \Lysine\CookieContextHandler($config); $expire = (int) $handler->get('expire'); if ($expire && $expire < time()) { $handler->clear(); } return $handler; }
public function testBindIpCookieContext() { $mock_cookie = \Test\Mock\Cookie::getInstance(); $mock_cookie->reset(); $config = array('token' => 'test', 'sign_salt' => 'fdajkfldsjfldsf', 'bind_ip' => true); $_SERVER['REMOTE_ADDR'] = '192.168.1.1'; $handler = new \Lysine\CookieContextHandler($config); $handler->set('test', 'abc'); $mock_cookie->apply(); $handler->reset(); $_SERVER['REMOTE_ADDR'] = '192.168.1.3'; $this->assertEquals($handler->get('test'), 'abc', '同子网IP取值'); $handler->reset(); $_SERVER['REMOTE_ADDR'] = '192.168.2.1'; $this->assertNull($handler->get('test'), '不同子网IP取值'); }