PHP 7.2警告: “Cannot change session name when session is active”
项目在php7.1 版本正常,迁移服务器php 7.2 报错
Maybe it's a warning but with Tracy I have an error.
To solve this I put
session_write_close();
beforesession_set_cookie_params();
. It's not a good fix but it's works
session_name()
as well assession_set_cookie_params()
are always nonesense if the session is already running.翻译: session_name()以及
session_set_cookie_params
()总是不存在意义的,如果会话已在运行。
I had a similar problem but finally found a way through. The code below was my first approach that gave me errors.
-
static function startmysession($lifetime, $path, $domain, $secure, $httponly){
-
-
session_set_cookie_params($lifetime, $path, $domain, $secure, $httponly);
-
session_regenerate_id(true);
-
-
if(!isset($_SESSION)){
-
session_start();
-
}
-
}
Now Earlier versions of php overlooked our mistake(We were practically renaming and giving a session that already exists properties which is very wrong. So how did i solve this problem?(现在php的早期版本忽略了我们的错误(我们实际上是在重命名和提供一个已经存在的会话属性,这是非常错误的)。那么我是如何解决这个问题的呢?)
-
static function startmysession($lifetime, $path, $domain, $secure, $httponly){
-
if(!isset($_SESSION)){
-
session_set_cookie_params($lifetime, $path, $domain, $secure, $httponly);
-
@session_regenerate_id(true);
-
session_start();
-
}
-
}
I now bound the session_set_cookie_params()
just before session start and I test if the session already exists before doing so.(现在,我在会话开始之前绑定了会话session_set_cookie_params
(),并在此之前测试会话是否已经存在。)
My solution:
Originally:
-
$cookieParams = session_get_cookie_params();
-
-
session_set_cookie_params(
-
$seconds,
-
$cookieParams['path'],
-
$cookieParams['domain'],
-
$cookieParams['secure']
-
);
Now:
-
if(isset($_SESSION)) {
-
if ($seconds != 0) {
-
setcookie(session_name(), session_id(), time() + $seconds);
-
} else {
-
setcookie(session_name(), session_id(), $seconds);
-
}
-
} else {
-
$cookieParams = session_get_cookie_params();
-
-
session_set_cookie_params(
-
$seconds,
-
$cookieParams['path'],
-
$cookieParams['domain'],
-
$cookieParams['secure']
-
);
-
}
参考:PHP 7.2 Warning: "Cannot change session name when session is active" - Stack Overflow
文章来源: blog.csdn.net,作者:lxw1844912514,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/lxw1844912514/article/details/115113162
- 点赞
- 收藏
- 关注作者
评论(0)