3

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/
Nikos
  • 696
  • 2
  • 9
  • 23
  • Set up a single test.php file with no dependencies and try calling that. Make sure you have a file logger attached. – JohnP Apr 12 '11 at 15:49
  • one.php has no dependencies, it contains only the code given, and logs a message each time executed – Nikos Apr 12 '11 at 16:17

3 Answers3

0

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.

elvenbyte
  • 776
  • 1
  • 17
  • 34
  • I am pretty sure this behavior is because of my local configuration (which I did not tweak at all except the virtual host in apache, I installed tasksel/LAMP), but I would like to know why – Nikos Apr 12 '11 at 16:24
  • Can you post your httpd.conf file? Perhaps we can see the reason, thats your main config file in Apache. – elvenbyte Apr 13 '11 at 07:26
0

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

Nikos
  • 696
  • 2
  • 9
  • 23
0

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!

r.batinov
  • 26
  • 4