FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Form Hit Counter





moejoe
Hi Guys.

How would you make a hit counter on how many requests have been sent?
Requests are sent via a POST (the method) form to a php script.
I want, on my frontpage, to have a script saying how much forms have been sent, not using SQL tho, jst a PHP.

Thanks in advance.
Ducksteina
That's pretty simple.
You can use a text file where a number is stored. For that, you just need the functions fopen, fread and fwrite. If you still got problems putting these pieces together, I'll add an example later.

Edit:
You may use this code:
Code:
<?PHP
    $filename = 'counter.txt';
    $file = fopen($filename, "r+");
    $count = fread($file, filesize($filename));
    echo $count." views";
    fwrite($file,$count+1);
    fclose($file);
?>
Stubru Freak
This would be a better example:

Code:
<?php
$filename = 'counter.txt';
$file = fopen($filename, "r+");
fwrite($file,($count = fread($file, filesize($filename)))+1);
fclose($file);
echo $count . " views";
?>
Ducksteina
It's the same code. I think it doesn't matter if you have 5 lines or 6 lines Wink
Richard92
here is the 1st part (including the form)
Code:
<?php
echo"<form method=\"post\">
<input maxLength=256 name=texte value= size=50><br />
<input type=submit value=\"Send\" name=send></form>";


and here is the 2nd part (writing on a file and say how many views)
Code:

    $filename = 'counter.cfg';
    $file = fopen($filename, "r+");
    $count = fread($file, filesize($filename));
    echo "$count views";
if (isset($_POST[send])){
    fwrite($file,$count+1);
         }
    fclose($file);
?>


Put these codes in a blank index.php file
Stubru Freak
Ducksteina wrote:
It's the same code. I think it doesn't matter if you have 5 lines or 6 lines Wink



But it looks cooler :p
moejoe
Hi Guys, Thanks for your help!

What should i use? the counter.cfg or views.txt? Which is easier.

Warning: fopen(count.txt): failed to open stream: Permission denied
Warning: fread(): supplied argument is not a valid stream resource
Warning: fclose(): supplied argument is not a valid stream resource

Do i need to change my permissions?

I used Richards code.

Again thanks for all your help!
Ducksteina
moejoe wrote:
Hi Guys, Thanks for your help!

What should i use? the counter.cfg or views.txt? Which is easier.

Warning: fopen(count.txt): failed to open stream: Permission denied
Warning: fread(): supplied argument is not a valid stream resource
Warning: fclose(): supplied argument is not a valid stream resource

Do i need to change my permissions?

I used Richards code.

Again thanks for all your help!

You have to give the file chmod 777 or 755 permissions.
moejoe
Hey,
Thanks yer i changed it to 777.

I'm now using Stubru Freak code.

it works. But it adds more than +1 to the file.

It adds another digit.

So it start at 1
Then 12
Then 1213
The 12131214...so on

Quote:
<?php
$filename = 'counter.txt';
$file = fopen($filename, "r+");
fwrite($file,($count = fread($file, filesize($filename)))+1);
fclose($file);
echo $count . " views";
?>



I broke up the fwrite from the fread and now it doubles in value until 8, then jumps to 17, they stays on 18.
Thanks
asiddle423
there is actually a slight difference between these 2 codes. *I think*

Code 1 from Ducksteina
Code:

<?PHP
    $filename = 'counter.txt';
    $file = fopen($filename, "r+");
    $count = fread($file, filesize($filename));
    echo $count." views";
    fwrite($file,$count+1);
    fclose($file);
?>


and

Code 2 from Stubru Freak
Code:

<?php
$filename = 'counter.txt';
$file = fopen($filename, "r+");
fwrite($file,($count = fread($file, filesize($filename)))+1);
fclose($file);
echo $count . " views";
?>


code 1 will display the hits first before it is updated
and
code 2 will update the hits and then show the hits

if this is true then code 1 will always show 1 hit behind whereas code 2 will show the actual hits
so i would go with code 2 unless you want it to display as i have said for code 1

if this isn't true then please tell me im wrong it just looks that way from looking at the code
moejoe
Those codes don't +1 to the value, they seem to +1 to the filesize.

As i said the Views would grow like:
1
12
1213...
Then into the millions.

What should i change?[/quote]
Ducksteina
Oh, that was a real stupid mistake!

This should be working:
Code:
<?PHP
   $filename = 'counter.txt';
   $file = fopen($filename, "r+");
   $count = fread($file, filesize($filename)) + 1;
   echo $count." views";
   fclose($file);
   $file = fopen($filename, "w");
   fwrite($file,$count);
   fclose($file);
?>

The problem was it didn't clear the old file. So it just added char per char, and not +1.
moejoe
Surprised ! Works 100%.

Cool As Thank You!
asiddle423
Oh right i never spotted that
and would it increment on the 1st code after it had been displayed
and incremented before it was displayed on the 2nd code
moejoe
Cool.
Thanks for all your help guys.

How would you save more information?
Like the persons name.

would you have to make another .txt file? or can it be done on the same .txt?

Thanks.
Related topics
Passwords?
how many visitors you get per day ?
A Few Tutorals I Have Wrote
Phoenix free PHP scripts
frame stuff
rats
'the finger' slipped into work pics...
my site...CHECK IT OUT!!!
Hit counter
Where can i learn...
anyone know of a good php form processer script
Simple, simple HIT counter
joomla and hit counter
Extremely Low Costing phpBB2 and 3 Services [UPDATED]
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.