每个浏览器请求都会执行多次 php 脚本
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想您尝试过,但是...,您是否尝试过从真实服务器而不是本地主机进行操作?我的意思是,问题可能出在你的 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.
我已经重新安装了服务器,现在一切正常。
I've reinstalled the server and everything works fine now.
我也有同样的问题。我为网站的所有网页设置了一个简单的访客计数器,并记录了回访者的信息。
其中一个页面是.../reviews/1(其中/1是reviews.php脚本的传递参数)和我的Reviews.php 脚本被执行了甚至 5 次,并且在这 5 次之后,index.php 脚本被执行。因此,即使我们打开评论页面,我最终还是在每次评论页面访问时,评论页面的计数器增加了 5 次,索引页面的计数器增加了 1 次。 (这太疯狂了!)
我尝试回显
$_SERVER['REQUEST_URI']
来检查请求是什么,但出现了 2 个问题)
。 (请注意,资产之前的/斜杠),服务器在执行请求时尝试访问这些图像,如下所示
http://demo_server/reviews/1 /assets/test.png
。因此,它认为 assets/test.png 为 reviews.php 脚本添加了额外的请求参数,并再次执行了该脚本。http://demo_server/favicon.ico
。我在 Chrome 中尝试过此操作 - 有问题,但在 Firefox 中 - 没有!并没有帮助我和 index.php 脚本再次执行。
.htaccess
帮助我阻止请求,而不是加载整个 index.php 脚本并进行我的评论页面性能缓慢:所以最后,我的传奇被揭露了,计数器只增加了一次,因为它应该起作用。
我分享这段经验,希望它能帮助某人或为他或她分析 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:<img>
tags with some images (<img src="assets/test.png">
)<img src="**/**assets/test.png">
. (Note that / slash before the assets) , the server tried to access these images as it executed the request like thishttp://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.<img src="/assets/test.png">
http://demo_server/favicon.ico
. I tried this in Chrome - there was a problem, but in Firefox - there wasn't!<link rel="shortcut icon" href="data:" />
didn't helped me and the index.php script was executed again..htaccess
helped me to block the request and not to load the whole index.php script and make slow performance of my reviews page: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!