警告: session_encode(): 无法在站点中编码不存在的会话(已启动会话)

huangapple go评论76阅读模式
英文:

Warning: session_encode(): Cannot encode non-existent session in site (Have started Session)

问题

我在一个php文件(Logit.php)中有以下代码,并从中调用下面的日志记录函数:

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

function logging($string){
// 其他操作
    $SESSIONData = session_encode(); // 字段长度:8192
// 其他操作
}

包含此日志记录脚本的php文件正在作为一个cron作业运行,有时我在命令行中运行它。我在我的Apache日志中收到以下错误:

警告:session_encode():无法编码不存在的会话位于
/var/www/example.com/Logit.php

对于为什么会出现这个错误,有什么想法吗?我找到的与此错误相关的信息非常有限,也没有解决此问题的方法。在Stack Overflow上,我只找到了三个与之有关的问题,而且关联性很小。

英文:

I have the following code in a php file (Logit.php) and I call the logging function below from it:

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

function logging($string){
// Other stuff
	$SESSIONData = session_encode(); // Field Length: 8192
// Other stuff
}

The php file that includes this logging script is running as a cron job and sometimes I run it at the command line. I get the following error in my Apache logs:

> Warning: session_encode(): Cannot encode non-existent session in
> /var/www/example.com/Logit.php

Any thoughts on why, and how can I remove this error? Information related to this error is very sparse and doesn't address this issue. I got only three hits on SO that were even remotely related.

答案1

得分: 1

根据PHP文档,如果会话被禁用(在您的情况下,非Web服务器脚本很可能是这样),session_status() 可能会返回 PHP_SESSION_DISABLED。如果您的脚本在禁用会话的环境中运行,那么这正是您将获得的行为。session_start 没有被调用,因为 session_status() === PHP_SESSION_NONE 为假(所以您不会在这里得到错误),并且 session_encode() 在没有会话的情况下被调用(因为它们无法存在,因为被禁用了),导致错误。

英文:

According to the PHP docs, session_status() might return PHP_SESSION_DISABLED if sessions are disabled (which is likely for non-webserver scripts, as in your case). If your script is run in an environment with sessions disabled, this is exactly the behaviour you would get. session_start isn't called because session_status() === PHP_SESSION_NONE is false (so you don't get an error there), and session_encode() is called without a session (which can't exist anyway because they are disabled), resulting in an error

huangapple
  • 本文由 发表于 2023年6月12日 18:50:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76455916.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定