使用PHP中的错误日志来调试代码我遇到了以下情况,看起来好像同一脚本的两个实例并行运行。我对此持怀疑态度,因此我请求您的支持帮助我弄清楚发生了什么。编辑:以下情况发生在为单个用户提供服务的开发服务器中。
以下是我的index.php脚本中运行的代码的摘录(我已经简化了,因为它更长)。关键是我正在启动会话然后重定向。用/ ** /划分的行是我添加的行以将数据添加到错误日志中:
session_start();
if(!isset($_SESSION['check'])) //session initialization code
{
/*1*/static $thread = 0;
/*2*/error_log("\nInit block - Run: " . $thread++);
/*3*/error_log("\nInto init block - URI: " . $_SERVER['REQUEST_URI'] . "Time: " . microtime());
$_SESSION['check'] = true;
/*4*/error_log("\nInit block - Run: " . $thread++); /*execution of second script instance seems to start at this point*/
$_SESSION['var1'] = /*var1 initialization code*/;
/*5*/error_log("\nInit block - Run: " . $thread);
$_SESSION['var2'] = /*var 2 initialization code*/;
/*6*/error_log("\nInit block - Run: " . $thread);
$_SESSION['var3'] = /*var3 initialization code*/;
/*7*/error_log("\nInit block - Run: " . $thread);
$_SESSION['var4'] = /*var4 intialization code*/
/*8*/error_log("\nInit block - Run: " . $thread);
$_SESSION['var5'] = /*var5 initialization code*/
/*9*/error_log("\nInit block - Run: " . $thread++);
}
if(/*condition1*/)
{
/*10*/static $filter = 0;
/*11*/error_log("\nInto condition 1 - URI: " . $_SERVER['REQUEST_URI'] . "Time: " . microtime());
/*12*/error_log("\nCondition 1 - Run: " . $filter);
header("Location: " . $targetUrl . $resultPage); //Post->Redirect->Get
exit();
}
else
{
/*...*/
}
错误日志中的输出如下:我正在附加生成输出的代码行,以减少阅读的麻烦,希望如下:
[05-Apr-2017 19:17:25]
Init block - Run: 0 /*2*/
[05-Apr-2017 19:17:25]
Into init block - URI: / Time: 0.59423800 1491430645 /*3*/
[05-Apr-2017 19:17:25]
Init block - Run: 1 /*4*/
[05-Apr-2017 19:17:25]
Init block - Run: 0 /*2*/
[05-Apr-2017 19:17:25]
Into init block - URI: / Time: 0.70579800 1491430645 /*3*/
[05-Apr-2017 19:17:25] /*4*/
Init block - Run: 1
[05-Apr-2017 19:17:28]
Init block - Run: 2 /*5*/
[05-Apr-2017 19:17:28]
Init block - Run: 2 /*6*/
[05-Apr-2017 19:17:28]
Init block - Run: 2 /*7*/
[05-Apr-2017 19:17:28]
Init block - Run: 2 /*8*/
[05-Apr-2017 19:17:28]
Init block - Run: 2 /*9*/
[05-Apr-2017 19:17:28]
Into condition 1 - URI: / Time: 0.94149400 1491430648 /*11*/
[05-Apr-2017 19:17:28]
Condition 1 - Run: 0 /*12*/
[05-Apr-2017 19:17:28]
Init block - Run: 2 /*5*/
[05-Apr-2017 19:17:28]
Init block - Run: 2 /*6*/
[05-Apr-2017 19:17:29]
Init block - Run: 2 /*7*/
[05-Apr-2017 19:17:29]
Init block - Run: 2 /*8*/
[05-Apr-2017 19:17:29]
Init block - Run: 2 /*9*/
[05-Apr-2017 19:17:29]
Into condition 1 - URI: / Time: 0.09837300 1491430649 /*11*/
正如您所看到的,代码执行到行/ * 4 * /,当它似乎被新脚本实例中断时,并且仅在新实例结束执行后才恢复。我对头函数调用持怀疑态度,但在此之前流程被中断了。在尝试找到答案时,我发现根据Apache的设置,日志记录可能会重复,但正如您所看到的,这不是双重日志记录的情况。任何帮助将不胜感激!