Log in

View Full Version : Help with simple text-based PHP hit counter



Iconoclast
03-21-2009, 07:43 PM
Hi guys, I'm trying to put a really simple text-based PHP hit counter in the footer of my page...but I'm getting two PHP errors when I use what I'm trying to do:

Here is the section of code in my footer that has all the basic footer info:


<div id="footer">

<div class="left">&copy;2001-2009 <a href="http://paulritter.net">paulritter.net</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Valid <a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a> &amp; <a href="http://validator.w3.org/check?uri=referer">XHTML</a></div>

<div class="right">Design and coding by <a href="http://paulritter.net/about">Paul Ritter</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="<?=$this->url('/login')?>"><?=t('&lt; Login &gt;')?></a></div>

<br /><div class="right">Total Hits:
<?

$filename = "hit_counter.txt";
@$fptr = fopen($filename, "r+");

if ($fptr == NULL) {
@$fptr = fopen($filename, "w+");
fwrite($fptr, "1");
fclose($fptr);
echo "1";
}
else {
$data = fread($fptr, filesize($filename));
$dataInt = (int) $data;
$dataInt++;
rewind($fptr);
fwrite($fptr, $dataInt);
fclose($fptr);
echo $dataInt;
}
?></div>

<div class="clearer"><span></span></div>

</div>

</div>

And I have a file called "hit_counter.txt" in the SAME directory as this PHP file. I have chmodded it to 777, so it should be writable.

Here are the errors I'm getting:


Warning: fwrite(): supplied argument is not a valid stream resource in /home/a9712442/public_html/themes/halloween/default.php on line 35

Warning: fclose(): supplied argument is not a valid stream resource in /home/a9712442/public_html/themes/halloween/default.php on line 36

Line 35 & 36 are these lines:


fwrite($fptr, "1");
fclose($fptr);

I looked up the error, and I thought it would be a permissions issue...but after chmodding the hit_counter.txt file to 777, I can't think why this error still occurs. Any help would be greatly appreciated! :D

Schmoopy
03-21-2009, 09:13 PM
It's definitely to do with the file permission, I just ran your code on my local machine and it worked perfectly, with no errors. Not sure what you should change the permission to though, maybe google around a bit more.
At least you know your code works now :)

Iconoclast
03-21-2009, 09:31 PM
Someone on a different forum suggested removing the @ before $fptr, which yields these errors:


Warning: fopen(hit_counter.txt) [function.fopen]: failed to open stream: No such file or directory in /home/a9712442/public_html/themes/halloween/default.php on line 31

Warning: fopen(hit_counter.txt) [function.fopen]: failed to open stream: Permission denied in /home/a9712442/public_html/themes/halloween/default.php on line 34

Warning: fwrite(): supplied argument is not a valid stream resource in /home/a9712442/public_html/themes/halloween/default.php on line 35

Warning: fclose(): supplied argument is not a valid stream resource in /home/a9712442/public_html/themes/halloween/default.php on line 36

JasonDFR
03-21-2009, 10:13 PM
Those last errors you posted are the key. I know you said that your hit_counter file is in the same directory as the script calling for it, but that is not correct. Try using the absolute path to the hit counter file. Something like '/home/a9712442/public_html/hit_counter.txt'

On line 31, you are trying to open a file that doesn't exist.

On line 34, you are trying to open or create the file, your script doesn't have write permission to the directory the file would be created in. You'll have to change the permissions of the directory as well as the file.

Also, fopen() returns false on error, so test for false instead of null.

Good luck.

Iconoclast
03-21-2009, 10:25 PM
Ahh, now I was able to fix it...it was looking for the hit_counter.txt file in my /public_html/ directory, while I thought it would look for it in the same directory that my PHP file was in. Thanks for all the help!