PHP 7.2警告: “Cannot change session name when session is active”

举报
lxw1844912514 发表于 2022/03/26 23:41:24 2022/03/26
【摘要】 项目在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(); before sessio...

项目在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(); before session_set_cookie_params();. It's not a good fix but it's works

session_name() as well as session_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.


  
  1. static function startmysession($lifetime, $path, $domain, $secure, $httponly){
  2. session_set_cookie_params($lifetime, $path, $domain, $secure, $httponly);
  3. session_regenerate_id(true);
  4. if(!isset($_SESSION)){
  5. session_start();
  6. }
  7. }

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的早期版本忽略了我们的错误(我们实际上是在重命名和提供一个已经存在的会话属性,这是非常错误的)。那么我是如何解决这个问题的呢?)


  
  1. static function startmysession($lifetime, $path, $domain, $secure, $httponly){
  2. if(!isset($_SESSION)){
  3. session_set_cookie_params($lifetime, $path, $domain, $secure, $httponly);
  4. @session_regenerate_id(true);
  5. session_start();
  6. }
  7. }

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:


  
  1. $cookieParams = session_get_cookie_params();
  2. session_set_cookie_params(
  3. $seconds,
  4. $cookieParams['path'],
  5. $cookieParams['domain'],
  6. $cookieParams['secure']
  7. );

Now:


  
  1. if(isset($_SESSION)) {
  2. if ($seconds != 0) {
  3. setcookie(session_name(), session_id(), time() + $seconds);
  4. } else {
  5. setcookie(session_name(), session_id(), $seconds);
  6. }
  7. } else {
  8. $cookieParams = session_get_cookie_params();
  9. session_set_cookie_params(
  10. $seconds,
  11. $cookieParams['path'],
  12. $cookieParams['domain'],
  13. $cookieParams['secure']
  14. );
  15. }

参考: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

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。