PHP似乎执行了两次脚本。

时间:2022-06-03 04:49:32

My web server is acting wierd. It seems like it executes scripts (PHP) twice before sending then to apache.

我的web服务器正在代理wierd。看起来它在发送给apache之前执行了两次脚本(PHP)。

I've run this file:

我运行这个文件:

<?php
echo '<pre>';
session_start();
$_SESSION['index']++;
echo '<br>';
print_r($_SESSION);
?>

And I know that ++ will give a notice at first, but it's just to investigate if it runs twice. Anyway, the printed session shows that the index-index increases by two each time you load the page.

我知道++一开始会给出一个通知,但它只是检查它是否运行了两次。无论如何,打印的会话显示,每次加载页面时,索引索引增加2。

The webserver is apache2, php5 installed on a debian unit.

web服务器是apache2, php5安装在debian单元上。

Anyone has any ideas?

任何人有任何想法吗?

4 个解决方案

#1


5  

echo '<pre>'; //Headers and cookies already sent.
session_start(); //Cannot set cookie anymore, system tries again, I guess

Start session first, then output anything. Try placing session_start(); on top

首先启动会话,然后输出任何内容。试着把session_start();在上面

#2


2  

I can't thank the poster of this question enough. His session test made me realize that I had the same problem of a php script running several times.

我对这个问题的海报感激不尽。他的会话测试让我意识到,我有同样的问题,一个php脚本运行了好几次。

In my script I had two PDO functions seperated from each other by an if-else-construct. One was a simple select, and one a simple insert function. But everytime I ran the script, both pdo commands got executed. PDO ended up writing rows in my table which had the entry 'public'.

在我的脚本中,我有两个PDO函数,它们是由一个if-else-构造函数分离出来的。一个是简单的选择,一个是简单的插入函数。但是每次运行脚本时,都会执行两个pdo命令。PDO最终在我的表中写入了有“public”条目的行。

So what happened? My page got send multiple times because of ELEMENTS IN THE HTML CODE THAT COULD NOT BE FOUND. In my case that was a css file which was named incorrectly. When I solved that (after 4 hours of looking at code) the problem disappeared. Also broken images for example trigger the same event.

那么发生了什么?由于HTML代码中的元素无法找到,我的页面被多次发送。在我的例子中,这是一个被错误命名的css文件。当我解决了这个问题(在看了4个小时代码之后),问题就消失了。同样损坏的图像也会触发相同的事件。

#3


0  

I am seeing the same behaviour... a $_POST would be present the first time the page ran, then wouldn't the second time... scoured the code to find why the page might be posting back to itself again. No avail.

我看到了同样的行为……$_POST将在页面第一次运行时出现,然后不会在第二次运行时出现……搜索代码,找出为什么页面可能会重新发布回自己。都无济于事。

After seeing user1601869's answer above, I started checking. I had some links to stylesheets I hadn't written yet, so put skeletons of those in.

在看到上面user1601869的答案后,我开始检查。我有一些我还没写过的样式表的链接,所以把它们的骨架放进去。

It turns out that for me, the culprit was:

对我来说,罪魁祸首是:

<link rel="shortcut icon" href="">

This was just a placeholder for an icon that was causing my page to break. I suggest that if you have the same problem, look for links in the <HEAD></HEAD> that are broken!

这只是导致页面崩溃的图标的占位符。我建议,如果您有同样的问题,在中寻找链接,它是坏的!

#4


0  

Oke folks, found a completely insane solution to this problem. Just posting for future reference. I recently installed a HTML validator in Chrome (an extension). This seems to be the culprit. After everything has loaded, the validator seems to be re-requesting the page so it is executed twice. Nice plugin. Not! Took me about half a day to figure this out.

奥克人,找到了一个完全疯狂的解决方案。仅供日后参考。我最近在Chrome(一个扩展)中安装了一个HTML验证器。这似乎是罪魁祸首。加载完所有内容后,验证器似乎正在重新请求页面,因此它将执行两次。不错的插件。不!我花了半天时间才搞清楚。

#1


5  

echo '<pre>'; //Headers and cookies already sent.
session_start(); //Cannot set cookie anymore, system tries again, I guess

Start session first, then output anything. Try placing session_start(); on top

首先启动会话,然后输出任何内容。试着把session_start();在上面

#2


2  

I can't thank the poster of this question enough. His session test made me realize that I had the same problem of a php script running several times.

我对这个问题的海报感激不尽。他的会话测试让我意识到,我有同样的问题,一个php脚本运行了好几次。

In my script I had two PDO functions seperated from each other by an if-else-construct. One was a simple select, and one a simple insert function. But everytime I ran the script, both pdo commands got executed. PDO ended up writing rows in my table which had the entry 'public'.

在我的脚本中,我有两个PDO函数,它们是由一个if-else-构造函数分离出来的。一个是简单的选择,一个是简单的插入函数。但是每次运行脚本时,都会执行两个pdo命令。PDO最终在我的表中写入了有“public”条目的行。

So what happened? My page got send multiple times because of ELEMENTS IN THE HTML CODE THAT COULD NOT BE FOUND. In my case that was a css file which was named incorrectly. When I solved that (after 4 hours of looking at code) the problem disappeared. Also broken images for example trigger the same event.

那么发生了什么?由于HTML代码中的元素无法找到,我的页面被多次发送。在我的例子中,这是一个被错误命名的css文件。当我解决了这个问题(在看了4个小时代码之后),问题就消失了。同样损坏的图像也会触发相同的事件。

#3


0  

I am seeing the same behaviour... a $_POST would be present the first time the page ran, then wouldn't the second time... scoured the code to find why the page might be posting back to itself again. No avail.

我看到了同样的行为……$_POST将在页面第一次运行时出现,然后不会在第二次运行时出现……搜索代码,找出为什么页面可能会重新发布回自己。都无济于事。

After seeing user1601869's answer above, I started checking. I had some links to stylesheets I hadn't written yet, so put skeletons of those in.

在看到上面user1601869的答案后,我开始检查。我有一些我还没写过的样式表的链接,所以把它们的骨架放进去。

It turns out that for me, the culprit was:

对我来说,罪魁祸首是:

<link rel="shortcut icon" href="">

This was just a placeholder for an icon that was causing my page to break. I suggest that if you have the same problem, look for links in the <HEAD></HEAD> that are broken!

这只是导致页面崩溃的图标的占位符。我建议,如果您有同样的问题,在中寻找链接,它是坏的!

#4


0  

Oke folks, found a completely insane solution to this problem. Just posting for future reference. I recently installed a HTML validator in Chrome (an extension). This seems to be the culprit. After everything has loaded, the validator seems to be re-requesting the page so it is executed twice. Nice plugin. Not! Took me about half a day to figure this out.

奥克人,找到了一个完全疯狂的解决方案。仅供日后参考。我最近在Chrome(一个扩展)中安装了一个HTML验证器。这似乎是罪魁祸首。加载完所有内容后,验证器似乎正在重新请求页面,因此它将执行两次。不错的插件。不!我花了半天时间才搞清楚。