Jump to content

[SOLVED] Help with simple text-based PHP hit counter


Iconoclast

Recommended Posts

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

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

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

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!

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

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.