Results 1 to 5 of 5

Thread: Help with simple text-based PHP hit counter

  1. #1
    Join Date
    Dec 2008
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Help with simple text-based PHP hit counter

    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:

    Code:
    	<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:

    Code:
        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!

  2. #2
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    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

  3. #3
    Join Date
    Dec 2008
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default

    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

  4. #4
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    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.

  5. #5
    Join Date
    Dec 2008
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default

    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!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •