使用Symfony会话';s`migrate`方法在PHP7.2上引发错误
我们在Symfony应用程序上使用Guard验证器通过外部应用程序进行身份验证。在我们的使用Symfony会话';s`migrate`方法在PHP7.2上引发错误,php,symfony,php-7.2,Php,Symfony,Php 7.2,我们在Symfony应用程序上使用Guard验证器通过外部应用程序进行身份验证。在我们的AbstractGuardAuthenticator实现的getCredentials方法中,我们对请求的会话使用migrate方法来刷新会话 /** * Called on every request and refreshes the user's session. * Attempts to get credentials (i.e. a token) from a request: *
AbstractGuardAuthenticator
实现的getCredentials
方法中,我们对请求的会话使用migrate
方法来刷新会话
/**
* Called on every request and refreshes the user's session.
* Attempts to get credentials (i.e. a token) from a request:
* If a token is present, attempts to log a user in. Continues to the getUser method.
* If there is no token a user is logged in, the request continues.
* If there is no token and there is no logged in user, sends the User to authenticate.
*
* @param Request $request The current request
* @return array|null If there is a token present, it is returned in an array; otherwise null is returned
*/
public function getCredentials(Request $request)
{
// Update the session lifetime
// The migrate method regenerates the session to a new session id, but preserves
// all attributes. The first parameter is set to true so that the old session
// is destroyed, and the second parameter resets the lifetime to 30 minutes.
$request->getSession()->migrate(true, 30 * 60);
// Check query params for token
$token = $request->query->get('token');
// When no token is provided:
// If user is logged in, skip authentication steps
// If no user is logged in, go to start method to authenticate
if($token == null)
return null;
// Token provided, continue to getUser
return ['token' => $token];
}
当在PHP7.1上运行时,这可以正常工作。migrate
方法返回true,表示迁移成功。在PHP 7.2上运行时,出现以下错误:
Symfony\Component\Debug\Exception\ContextErrorException:
Warning: ini_set(): A session is active. You cannot change the session module's ini settings at this time
at vendor\symfony\symfony\src\Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage.php:201
at Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage->regenerate(true, 1800)
(vendor\symfony\symfony\src\Symfony\Component\HttpFoundation\Session\Session.php:185)
at Symfony\Component\HttpFoundation\Session\Session->migrate(true, 1800)
(vendor\OrgOne\authentication-bundle\OrgOne\AuthenticationBundle\Security\TokenAuthenticator.php:55)
at OrgOne\AuthenticationBundle\Security\TokenAuthenticator->getCredentials(object(Request))
(vendor\symfony\symfony\src\Symfony\Component\Security\Guard\Firewall\GuardAuthenticationListener.php:118)
我们应该如何在PHP7.2中使用
migrate
方法或类似的方法?我可以用PHP7.2.7和symfony 4.2重现这种行为
如果在没有参数的情况下调用migrate
函数,则不会生成异常
$request->getSession()->migrate()
然后将使用framework.yml
config中指定的生存期。因此,您可以将cookie_生存期设置为1800(=30*60)
framework:
...
session:
handler_id: session.handler.native_file
save_path: '%kernel.project_dir%/var/sessions/%kernel.environment%'
cookie_lifetime: 1800