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.
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);
?> |
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";
?> |
It's the same code. I think it doesn't matter if you have 5 lines or 6 lines 
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
| Ducksteina wrote: |
It's the same code. I think it doesn't matter if you have 5 lines or 6 lines  |
But it looks cooler :p
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!
| 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.
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
Last edited by moejoe on Thu Apr 06, 2006 5:34 pm; edited 1 time in total
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
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]
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.
! Works 100%.
Cool As Thank You!
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
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.