PHP includes_once.. multiple times?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
abelz
Forum Newbie
Posts: 2
Joined: Sat Feb 27, 2010 11:02 am

PHP includes_once.. multiple times?

Post by abelz »

Hello, and thank you for allowing me to post on this forum. I was wondering if anyone else has run into the problem I have been dealing with for two days now. I was making a very simple counter.php file. The object of it was to load a .dat file with a line-separated count for each page that is visited like so:

index(10)
content(30)
news(23)

I thought I had made it right until I realized that it was incrementing by 3 everytime I went to my page. I rewrote the script 3 different ways, tinkering for hours with each one thinking it was programming error I could not see. I made it write fileX where X was the page count and saw that it was definitely incrementing each one, just that it was running the same php file 3 times for each refresh. After a while I assumed php was including the file multiple times, even though I did not say it, so I set it to include_once. Finally I found a script online to test to see if it was my server, but the php script I downloaded and ran worked fine. I then started hacking my HTML code up, deleting here and there, until I found out that certain DIV elements that I deleted decreased the number of increments.. from 3 to 1.. I kept hacking away until finally I found out which div elements.. or rather which div CLASSes were causing the rror and I found this in two of my classes (hence, running the script 3 times):

background-image:url('');

Is this a known bug, and does this cause calls to the server for the same file each time? When I put a random letter in between the single quotes it stopped bugging and incremented by 1, but leaving it blank caused this error for some reason. Does anyone know why this is? I just wasted 4 hours of my life and I would like to at least gain a little kernel of knowledge from all that wasted time :|
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: PHP includes_once.. multiple times?

Post by califdon »

If I understand what you described, you are trying to count hits on a page by including a script file that increments a line in a file. Not necessarily the most efficient way to get a page count, it's usually done by analyzing the log file, but perhaps you have reasons to do it that way. Anyway, most browsers interpret a URL reference that is a zero-length string to be a reference to the currently displayed URL, such as is often done with HTML forms, to call the same script again to handle form data. So when you reference an image, but don't supply any URL, it will send another request to the server for the same page. So your counts were exactly correct, you were loading the same page 3 times. Of course, that page was NOT an image, so that's utterly wrong, to begin with. You should not have an image reference that doesn't point to an image file. But your script was counting accurately the number of times the server sent that file to the browser.
abelz
Forum Newbie
Posts: 2
Joined: Sat Feb 27, 2010 11:02 am

Re: PHP includes_once.. multiple times?

Post by abelz »

califdon wrote:If I understand what you described, you are trying to count hits on a page by including a script file that increments a line in a file. Not necessarily the most efficient way to get a page count, it's usually done by analyzing the log file, but perhaps you have reasons to do it that way. Anyway, most browsers interpret a URL reference that is a zero-length string to be a reference to the currently displayed URL, such as is often done with HTML forms, to call the same script again to handle form data. So when you reference an image, but don't supply any URL, it will send another request to the server for the same page. So your counts were exactly correct, you were loading the same page 3 times. Of course, that page was NOT an image, so that's utterly wrong, to begin with. You should not have an image reference that doesn't point to an image file. But your script was counting accurately the number of times the server sent that file to the browser.
Ahh, that makes a lot of sense now, actually. So it was self-referencing the index.php file in the background-img function's url var because it was set to nothing. I guess, then, it does not re-evaluate the php again because usually image files do not have php in them? lol
Alright, well thank you for the help, and I will look into doing page counts through the log file. I may eventually just shift everything to a database which will be much easier than parsing a text file.. I just find odd pleasure in parsing text files that I create :) Anyway, thank you again for helping me
Post Reply