每个浏览器请求都会执行多次 php 脚本

发布于 2024-10-31 23:54:58 字数 3165 浏览 6 评论 0原文

我在 apache 中有一个虚拟主机。我在 ubuntu 10.04 上使用 LAMP。

<VirtualHost *:80>
DocumentRoot /home/username/websites/site_folder
ServerName www.site_folder.com
ServerAlias site_folder.com
    <Directory /home/username/websites/site_folder/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>

我在 /etc/hosts 中添加了以下行:

127.0.0.1 site_folder.com

在虚拟主机的文件夹内,我添加了一个名为 one.php 的 php 脚本,其代码为:

<?php
$today = getdate();
$handle = fopen("logs/logs.txt", "a");
fwrite($handle, $today['mday'].'/'.$today['mon'].'/'.$today['year']." this is a log post"." \n");
fclose($handle);
?>

当我从浏览器运行该脚本时, http://localhost/one.php 或 site_folder.com/one.php,logs.txt 已记录相同消息 3 次:

12/4/2011 this is a log post 
12/4/2011 this is a log post 
12/4/2011 this is a log post 

使用 netbeans 调试器我看到该脚本实际上重复了 3 次(到达脚本末尾后,它从同一脚本的开头继续 ->one.php) 该文件夹内不存在 .htaccess。

我注意到 $_SERVER['REQUEST_URI'] 每次执行/重复都会发生一点变化:

1)$_SERVER['REQUEST_URI'] => /one.php?XDEBUG_SESSION_START=netbeans-xdebug
2)$_SERVER['REQUEST_URI'] => /one.php
3)$_SERVER['REQUEST_URI'] => /one.php

我只需要记录一次消息。

我添加了 apache2.conf 中未注释的指令:

ServerRoot "/etc/apache2"
LockFile /var/lock/apache2/accept.lock
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 15
<IfModule mpm_prefork_module>
    StartServers          5
    MinSpareServers       5
    MaxSpareServers      10
    MaxClients          150
    MaxRequestsPerChild   0
</IfModule>
<IfModule mpm_worker_module>
    StartServers          2
    MinSpareThreads      25
    MaxSpareThreads      75 
    ThreadLimit          64
    ThreadsPerChild      25
    MaxClients          150
    MaxRequestsPerChild   0
</IfModule>
<IfModule mpm_event_module>
    StartServers          2
    MaxClients          150
    MinSpareThreads      25
    MaxSpareThreads      75 
    ThreadLimit          64
    ThreadsPerChild      25
    MaxRequestsPerChild   0
</IfModule>
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
AccessFileName .htaccess
<Files ~ "^\.ht">
    Order allow,deny
    Deny from all
    Satisfy all
</Files>
DefaultType text/plain
HostnameLookups Off
ErrorLog /var/log/apache2/error.log
LogLevel warn
Include /etc/apache2/mods-enabled/*.load
Include /etc/apache2/mods-enabled/*.conf
Include /etc/apache2/httpd.conf
Include /etc/apache2/ports.conf
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
CustomLog /var/log/apache2/other_vhosts_access.log vhost_combined
Include /etc/apache2/conf.d/
Include /etc/apache2/sites-enabled/

I have a virtual host in apache. I am on ubuntu 10.04 using LAMP.

<VirtualHost *:80>
DocumentRoot /home/username/websites/site_folder
ServerName www.site_folder.com
ServerAlias site_folder.com
    <Directory /home/username/websites/site_folder/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>

I added in /etc/hosts the line:

127.0.0.1 site_folder.com

Inside the folder of the vhost I added a php script named one.php, having the code:

<?php
$today = getdate();
$handle = fopen("logs/logs.txt", "a");
fwrite($handle, $today['mday'].'/'.$today['mon'].'/'.$today['year']." this is a log post"." \n");
fclose($handle);
?>

when I run the script from the browser, http://localhost/one.php or site_folder.com/one.php, the logs.txt has logged the same message 3 times:

12/4/2011 this is a log post 
12/4/2011 this is a log post 
12/4/2011 this is a log post 

Using netbeans debugger I saw that that the script is actually repeated 3 times (after reaching the end of the script it continues from the start of the same script->one.php)
No .htaccess exists inside the folder.

What I have noticed is that the $_SERVER['REQUEST_URI'] changes a bit each execution/repetition:

1)$_SERVER['REQUEST_URI'] => /one.php?XDEBUG_SESSION_START=netbeans-xdebug
2)$_SERVER['REQUEST_URI'] => /one.php
3)$_SERVER['REQUEST_URI'] => /one.php

I need to log my message only once.

I added the uncommented directives from apache2.conf :

ServerRoot "/etc/apache2"
LockFile /var/lock/apache2/accept.lock
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 15
<IfModule mpm_prefork_module>
    StartServers          5
    MinSpareServers       5
    MaxSpareServers      10
    MaxClients          150
    MaxRequestsPerChild   0
</IfModule>
<IfModule mpm_worker_module>
    StartServers          2
    MinSpareThreads      25
    MaxSpareThreads      75 
    ThreadLimit          64
    ThreadsPerChild      25
    MaxClients          150
    MaxRequestsPerChild   0
</IfModule>
<IfModule mpm_event_module>
    StartServers          2
    MaxClients          150
    MinSpareThreads      25
    MaxSpareThreads      75 
    ThreadLimit          64
    ThreadsPerChild      25
    MaxRequestsPerChild   0
</IfModule>
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
AccessFileName .htaccess
<Files ~ "^\.ht">
    Order allow,deny
    Deny from all
    Satisfy all
</Files>
DefaultType text/plain
HostnameLookups Off
ErrorLog /var/log/apache2/error.log
LogLevel warn
Include /etc/apache2/mods-enabled/*.load
Include /etc/apache2/mods-enabled/*.conf
Include /etc/apache2/httpd.conf
Include /etc/apache2/ports.conf
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
CustomLog /var/log/apache2/other_vhosts_access.log vhost_combined
Include /etc/apache2/conf.d/
Include /etc/apache2/sites-enabled/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰魂雪魄 2024-11-07 23:54:58

我想您尝试过,但是...,您是否尝试过从真实服务器而不是本地主机进行操作?我的意思是,问题可能出在你的 Apache 服务器上......

我不知道,只是将其作为一个简单的想法来测试。

I suppose you tried it out, but..., have you tried to do it from a real server, instead a localhost one? I mean, the trouble maybe is in you Apache Server...

I don't know, just read it as a simple idea to test.

蓝眸 2024-11-07 23:54:58

我已经重新安装了服务器,现在一切正常。

I've reinstalled the server and everything works fine now.

夏末 2024-11-07 23:54:58

我也有同样的问题。我为网站的所有网页设置了一个简单的访客计数器,并记录了回访者的信息。

其中一个页面是.../reviews/1(其中/1是reviews.php脚本的传递参数)和我的Reviews.php 脚本被执行了甚至 5 次,并且在这 5 次之后,index.php 脚本被执行。因此,即使我们打开评论页面,我最终还是在每次评论页面访问时,评论页面的计数器增加了 5 次,索引页面的计数器增加了 1 次。 (这太疯狂了!)

我尝试回显 $_SERVER['REQUEST_URI'] 来检查请求是什么,但出现了 2 个问题

  1. : img> 标签包含一些图像 ()
    • 我忘记按如下方式编写链接。 (请注意,资产之前的/斜杠),服务器在执行请求时尝试访问这些图像,如下所示 http://demo_server/reviews/1 /assets/test.png。因此,它认为 assets/test.pngreviews.php 脚本添加了额外的请求参数,并再次执行了该脚本。
      • 当我在链接前面添加 / 并更正链接时,我解决了问题
  2. 第二个问题是执行index.php脚本,即使我没有链接或请求 **reviews.php 中的此页面
    • 问题在于浏览器默认请求 /favicon.ico 并执行以下 URL:http://demo_server/favicon.ico。我在 Chrome 中尝试过此操作 - 有问题,但在 Firefox 中 - 没有
    • 由于我的图标被组织在另一个文件夹中,并针对不同的像素和操作系统使用不同的名称,因此服务器默认尝试通过 index.php 页面获取 favicon.ico。因此,整个 index.php 页面在后台加载,这也需要时间!
    • 我尝试了此 StackOverflow 帖子中的一些答案:如何阻止 favicon.ico 请求? 但无缘无故最常见的答案 并没有帮助我和 index.php 脚本再次执行。
    • 将这些代码行添加到 .htaccess 帮助我阻止请求,而不是加载整个 index.php 脚本并进行我的评论页面性能缓慢:
      ; 
              重定向匹配 403 favicon.ico 
       
      

所以最后,我的传奇被揭露了,计数器只增加了一次,因为它应该起作用。

我分享这段经验,希望它能帮助某人或为他或她分析 Web 开发问题提供方向。

有时我们的小错误会造成更大的问题 - 正如我们在祖国所说的那样“石头打翻了车”

干杯,祝编码愉快!

I had the same problem. I had a simple visitors counter for all web pages of my website and logged info about returning visitors.

One of the pages was .../reviews/1 (where /1 is a passed parameter to the reviews.php script) and my reviews.php script was executed even 5 times, and after that 5 times, an index.php script was executed. So I finally got per every reviews page visit 5 more times increments in the counter of reviews page and 1 more time in the index page even though we open the reviews page. (that was insane!!)

I tried to echo $_SERVER['REQUEST_URI'] to check what the requests were and 2 problems appeared:

  1. I had <img> tags with some images (<img src="assets/test.png">)
    • I forgot to write the link as follows <img src="**/**assets/test.png">. (Note that / slash before the assets) , the server tried to access these images as it executed the request like this http://demo_server/reviews/1/assets/test.png. So it thought assets/test.png for additional request parameters to the reviews.php script and executed the script again.
      • I removed the problem as I corrected the links as I added that / in front of the link. <img src="/assets/test.png">
  2. The second problem was the executing of the index.php script even if I had no link or request to this page in the **reviews.php
    • The problem was that the browser requests /favicon.ico by default and executes this URL: http://demo_server/favicon.ico. I tried this in Chrome - there was a problem, but in Firefox - there wasn't!
    • As my favicons were organized in another folder and named with different names for the different pixels and OS, the server tried by default to get the favicon.ico through index.php page. So the entire index.php page was loaded in the background and this took time as well!
    • I tried some answers from this StackOverflow post: How to prevent favicon.ico requests? but for no reason the most common answer <link rel="shortcut icon" href="data:" /> didn't helped me and the index.php script was executed again.
    • Adding these lines of code into .htaccess helped me to block the request and not to load the whole index.php script and make slow performance of my reviews page:
      <IfModule mod_alias.c> 
              RedirectMatch 403 favicon.ico 
      </IfModule> 
      

So finally, my saga was revealed and the counter was incrementing only ONCE as it should work.

I share this experience with the hope that it will help someone or will give him a direction in analyzing his or her problem of web development.

Sometimes our little mistakes make our bigger problems - as we say in our native country "the stone overturned the cart".

Cheers and Happy Coding!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文