ohrstedtl Posted February 12, 2014 Share Posted February 12, 2014 I have encountered a strange error on a webpage using a textfile as a counter of visits on the page. Here is the code for a testpage: <code> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <!--created by Lennart Öhrstedt, Diadoker--> <html xmlns="http://www.w3.org/1999/xhtml"> <head> </head> <?php if (!file_exists("test.txt")) { $file=fopen("test.txt", "w"); fclose($file);} $file=fopen("test.txt", "r+"); while(!feof($file)) { $count=fgets($file)+1;} fclose($file); ?> <body> <img src="#" alt="test" height="100"> </body> </html> </code> With this code the count in the file "test.txt" is +2 insted of +1, if I change the src on the img-tag to anything other than #( src="#") like an A( src="A"), it counts correct. To check it out I added this code before ?> in the php section: <code> function alerten($texten) { echo "<script type='text/javascript' language='JavaScript'>alert('$texten');</script>";} $file=fopen("test.txt", "r+"); $count=fgets($file); fclose($file); alerten($count); </code> then the alert shows a correct count, after I clicked on the OK in the alert popup and look in the "test.txt" file the count is +1. The html tag <img> cannot access any file on the server, so this is a mystery for me. Is there an explanation for this behavior? Link to comment Share on other sites More sharing options...
justsomeguy Posted February 12, 2014 Share Posted February 12, 2014 Using # for the src is basically saying that the image source is the same HTML file, so the browser is going to send a second request to the same file. That's going to happen regardless of whether you use "#", or a question mark followed by anything, or just write the same filename into the src attribute. There's no reason to point the image source to the same file. If you want the image to have no source then leave it blank or remove the attribute, don't point it to the same file though. 1 Link to comment Share on other sites More sharing options...
ohrstedtl Posted February 12, 2014 Author Share Posted February 12, 2014 I was myself wundering if the webpage was accessed 2 times to explain the double count, thats why I used the adding code with an alert to check whether this was the fact, because if so then I should see 2 alert-windows, but I only see one, so can you explain this as well?. Link to comment Share on other sites More sharing options...
justsomeguy Posted February 12, 2014 Share Posted February 12, 2014 When the browser sends a request for an image, which is what the second request is, it's not going to execute the Javascript that it gets from the server. It's expecting an image, not a web page. 1 Link to comment Share on other sites More sharing options...
ohrstedtl Posted February 13, 2014 Author Share Posted February 13, 2014 OK. thanks for your explanation. I will take learning from this. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now