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升级时被覆盖,建议用自定义模块重写解决:
- 创建自定义模块(比如命名为
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>
- 创建重写类文件
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(); } }
- 执行以下命令启用模块并更新配置:
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
相关产品推荐
相关产品推荐

