Iconoclast Posted March 21, 2009 Share Posted March 21, 2009 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">©2001-2009 <a href="http://paulritter.net">paulritter.net</a> Valid <a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a> & <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> <a href="<?=$this->url('/login')?>"><?=t('< Login >')?></a></div> [color="Red"]<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>[/color] <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! Quote Link to comment https://forums.phpfreaks.com/topic/150498-solved-help-with-simple-text-based-php-hit-counter/ Share on other sites More sharing options...
WolfRage Posted March 21, 2009 Share Posted March 21, 2009 Get rid of the @ in front of your fopen() and let's see what the error is. Quote Link to comment https://forums.phpfreaks.com/topic/150498-solved-help-with-simple-text-based-php-hit-counter/#findComment-790454 Share on other sites More sharing options...
Iconoclast Posted March 21, 2009 Author Share Posted March 21, 2009 Which one? There are two that I can see: $filename = "hit_counter.txt"; [color=red]@$fptr = fopen($filename, "r+");[/color] if ($fptr == NULL) { [color=red] @$fptr = fopen($filename, "w+");[/color] Quote Link to comment https://forums.phpfreaks.com/topic/150498-solved-help-with-simple-text-based-php-hit-counter/#findComment-790461 Share on other sites More sharing options...
laffin Posted March 21, 2009 Share Posted March 21, 2009 follow the logic of yer code $filename = "hit_counter.txt"; @$fptr = fopen($filename, "r+"); if ($fptr == NULL) { @$fptr = fopen($filename, "w+"); fwrite($fptr, "1"); fclose($fptr); why wud u close the file, when a little later u write to it. there are simpler ways of doing all that code in simpler solution if(!($ctr=file_get_contents('counter.txt')) { $ctr=1; } else { $ctr++; } file_put_contents('counter.txt',$ctr); echo $ctr; Anyways good luck Quote Link to comment https://forums.phpfreaks.com/topic/150498-solved-help-with-simple-text-based-php-hit-counter/#findComment-790463 Share on other sites More sharing options...
Iconoclast Posted March 21, 2009 Author Share Posted March 21, 2009 Sorry, I'm not very good with PHP, I'm still learning...do I just need to replace my code with the code you posted above? Or is there more to it than that? Quote Link to comment https://forums.phpfreaks.com/topic/150498-solved-help-with-simple-text-based-php-hit-counter/#findComment-790465 Share on other sites More sharing options...
laffin Posted March 21, 2009 Share Posted March 21, 2009 U can replace this section $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; } as both are doing the same thing check if file counter exists if it doesnt exists, start with 1 if it does exists, increment counter save new counter echo counter. For not being good with php, you are doing fine But as programmer's know, there is more than one way to skin a cat. Good Luck agian Quote Link to comment https://forums.phpfreaks.com/topic/150498-solved-help-with-simple-text-based-php-hit-counter/#findComment-790468 Share on other sites More sharing options...
Iconoclast Posted March 21, 2009 Author Share Posted March 21, 2009 Now I get this error: Parse error: syntax error, unexpected '{' in /home/a9712442/public_html/themes/halloween/default.php on line 28 Line 28 is: if(!($ctr=file_get_contents('hit_counter.txt')) { Here's what it looks like (tell me if I'm doing something wrong): <div class="right">Total Hits: <? if(!($ctr=file_get_contents('hit_counter.txt')) { $ctr=1; } else { $ctr++; } file_put_contents('hit_counter.txt',$ctr); echo $ctr; ?> </div> Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/150498-solved-help-with-simple-text-based-php-hit-counter/#findComment-790475 Share on other sites More sharing options...
Iconoclast Posted March 21, 2009 Author Share Posted March 21, 2009 Also, here's the errors that appear when I remove the @ symbol from the $fptr: 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 Quote Link to comment https://forums.phpfreaks.com/topic/150498-solved-help-with-simple-text-based-php-hit-counter/#findComment-790511 Share on other sites More sharing options...
laffin Posted March 21, 2009 Share Posted March 21, 2009 looks like a missed an end paren if(!($ctr=file_get_contents('hit_counter.txt'))) { parens are good for seprating blocks, but they can get confusing when ya looking at how many levels they are Quote Link to comment https://forums.phpfreaks.com/topic/150498-solved-help-with-simple-text-based-php-hit-counter/#findComment-790515 Share on other sites More sharing options...
Iconoclast Posted March 21, 2009 Author Share Posted March 21, 2009 Okay I tried that...now I get these errors: Warning: file_get_contents(hit_counter.txt) [function.file-get-contents]: failed to open stream: No such file or directory in /home/a9712442/public_html/themes/halloween/default.php on line 29 Warning: file_put_contents(hit_counter.txt) [function.file-put-contents]: failed to open stream: Permission denied in /home/a9712442/public_html/themes/halloween/default.php on line 34 Line 29: if(!($ctr=file_get_contents('hit_counter.txt'))) { Line 34: } I really don't understand these errors...like I said, hit_counter.txt exists in that folder, and is chmodded to 777...isn't that right? Quote Link to comment https://forums.phpfreaks.com/topic/150498-solved-help-with-simple-text-based-php-hit-counter/#findComment-790517 Share on other sites More sharing options...
laffin Posted March 21, 2009 Share Posted March 21, 2009 yes, but for some reason it's not letting u access the file. not shure where to go from this point Quote Link to comment https://forums.phpfreaks.com/topic/150498-solved-help-with-simple-text-based-php-hit-counter/#findComment-790524 Share on other sites More sharing options...
Iconoclast Posted March 21, 2009 Author Share Posted March 21, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/150498-solved-help-with-simple-text-based-php-hit-counter/#findComment-790544 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.