PDA

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



Iconoclast
03-21-2009, 08:36 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

steelaz
03-21-2009, 10:15 PM
Remove @ from "@$fptr" and it will tell you why file cannot be opened/accessed. By Adding @ you are telling PHP not to show the error in case function fails.

Iconoclast
03-21-2009, 10:23 PM
Okay, I tried that...here are the errors I get now:



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

I also tried chmodding the default.php file to 777 just to see if anything changed, but it didn't do anything.

Any more ideas?

steelaz
03-21-2009, 10:32 PM
You don't have to chmod default.php. PHP says that there is no hit_counter.txt file in /home/a9712442/public_html/themes/halloween/ directory. Check if you named your file correctly and if it is in the same directory as default.php

Iconoclast
03-21-2009, 10:37 PM
Yes, hit_counter.txt exists in my /public_html/themes/halloween/ directory. That's why I don't get that error =/

(and it's chmodded to 777...read the very first post, I made sure to specify all that)

steelaz
03-21-2009, 11:17 PM
After $filename = "hit_counter.txt"; add



if (!file_exists($filename))
{
if ($handle = opendir(dirname($_SERVER['SCRIPT_FILENAME'])))
{
while (false !== ($file = readdir($handle)))
{
if (($file != '.') && ($file != '..')) echo "$file<br>";
}
closedir($handle);
}
exit(0);
}


It will check if file exists, if not, it will output all files in current directory.

Iconoclast
03-21-2009, 11: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!