Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Dec 2008
    Posts
    55
    Thanks
    18
    Thanked 0 Times in 0 Posts

    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
    New Coder
    Join Date
    Mar 2009
    Location
    Chicago, IL
    Posts
    69
    Thanks
    0
    Thanked 15 Times in 15 Posts
    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.

  • #3
    New Coder
    Join Date
    Dec 2008
    Posts
    55
    Thanks
    18
    Thanked 0 Times in 0 Posts
    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?

  • #4
    New Coder
    Join Date
    Mar 2009
    Location
    Chicago, IL
    Posts
    69
    Thanks
    0
    Thanked 15 Times in 15 Posts
    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

  • #5
    New Coder
    Join Date
    Dec 2008
    Posts
    55
    Thanks
    18
    Thanked 0 Times in 0 Posts
    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)

  • #6
    New Coder
    Join Date
    Mar 2009
    Location
    Chicago, IL
    Posts
    69
    Thanks
    0
    Thanked 15 Times in 15 Posts
    After $filename = "hit_counter.txt"; add

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

  • #7
    New Coder
    Join Date
    Dec 2008
    Posts
    55
    Thanks
    18
    Thanked 0 Times in 0 Posts
    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!


  •  

    Posting Permissions

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