You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Magento 2.2.1升级PHP7.3后出现session_id()警告,寻求解决方法

解决Magento 2.2.1升级PHP7.3后session_id()警告问题

临时快速修复

  • 打开路径 /home/customer/www/mysite.com/public_html/vendor/magento/framework/Session/SessionManager.php,找到第514行附近的session_id($sessionId)代码。
  • 给这段代码添加状态判断,修改为:
if (session_status() !== PHP_SESSION_ACTIVE) {
    session_id($sessionId);
}
  • 保存文件后,执行命令清除缓存:
bin/magento cache:flush

刷新网站即可看到警告消失。

永久修复(避免核心文件被覆盖)

直接修改vendor目录文件会在Magento升级时被覆盖,建议用自定义模块重写解决:

  1. 创建自定义模块(比如命名为YourName/SessionFix),在模块的etc/di.xml中添加配置:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Framework\Session\SessionManager" type="YourName\SessionFix\Model\Session\SessionManager" />
</config>
  1. 创建重写类文件YourName/SessionFix/Model/Session/SessionManager.php,内容如下:
<?php
namespace YourName\SessionFix\Model\Session;

use Magento\Framework\Session\SessionManager as OriginalSessionManager;

class SessionManager extends OriginalSessionManager
{
    public function start()
    {
        $sessionId = $this->getSessionId();
        if ($sessionId && session_status() !== PHP_SESSION_ACTIVE) {
            session_id($sessionId);
        }
        parent::start();
    }
}
  1. 执行以下命令启用模块并更新配置:
bin/magento module:enable YourName_SessionFix
bin/magento setup:upgrade
bin/magento cache:flush

问题原因

PHP7.3开始严格禁止在会话已激活的状态下调用session_id()修改会话ID,而Magento 2.2.1的核心代码未适配该版本限制,因此触发警告。若后续将Magento升级至2.3及以上版本,官方已修复此兼容性问题,无需再做上述修改。

内容的提问来源于stack exchange,提问作者robinlee222

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 11:04:59