Creating a Counter in PHP (Difficulty: Beginner)
Josh-
Royal Oak, MI
Creating a Counter in PHP
-- written by Josh
You will need to create 2 files for this guide;
First, copy and paste this to your counter.php file:
[php]
<?php
if($count == ""){
setcookie("count", "m");
$file = file("count.txt");
$num = ($file[0] + 1);
exec("echo $num > count.txt");
}
?>
[/php]
Now, lets decifer which does what, starting with
[php]$file = file("count.txt");[/php]
What this does it determines the file that every hit on your site will be recording to, this file can be changed or set to a variable if you wish to modify the code somewhat.
[php]
$num = ($file[0] + 1);
[/php]
After the $file variable is defined to count.txt, this function adds 1 to the number that is stored in your count.txt file. You could also amend it to subtract instead of add, ect.
Displaying the Number of Hits on your Page
[php]
exec("echo $num > count.txt");
[/php]
Displays the number recorded in your count.txt file on the page, it echoes (prints) the number onto your page.
[php]
if($count == ""){
setcookie("count", "m");
[/php]
This tries to make sure that your hit is not counted twice or more, ect.
I'd say thats pretty much all to know.
Have fun with your new site counter,
-Josh
-.-
-- written by Josh
You will need to create 2 files for this guide;
- count.txt - used to store hits on your site.
- counter.php - used to record hits on your site.
First, copy and paste this to your counter.php file:
[php]
<?php
if($count == ""){
setcookie("count", "m");
$file = file("count.txt");
$num = ($file[0] + 1);
exec("echo $num > count.txt");
}
?>
[/php]
Now, lets decifer which does what, starting with
[php]$file = file("count.txt");[/php]
What this does it determines the file that every hit on your site will be recording to, this file can be changed or set to a variable if you wish to modify the code somewhat.
[php]
$num = ($file[0] + 1);
[/php]
After the $file variable is defined to count.txt, this function adds 1 to the number that is stored in your count.txt file. You could also amend it to subtract instead of add, ect.
Displaying the Number of Hits on your Page
[php]
exec("echo $num > count.txt");
[/php]
Displays the number recorded in your count.txt file on the page, it echoes (prints) the number onto your page.
[php]
if($count == ""){
setcookie("count", "m");
[/php]
This tries to make sure that your hit is not counted twice or more, ect.
I'd say thats pretty much all to know.
Have fun with your new site counter,
-Josh
-.-
0
Comments
-- written by Josh
[php]
<?php
$filename = count.txt
if($count == ""){
setcookie("count", "m");
$file = file("$filename");
$num = ($file[0] + 1);
exec("echo $num > $filename");
}
?>
[/php]
Count.txt only has to be modified at $filename..rather then modifying it at multiple errors. For people who wish to modify the file that hits are recorded to, if the code is made more complex. Other then that, the code is pretty simple, but there are a lot of beginners that I guess could get confused.
-- Written by 02 from www.codexx.tk
[php]
<?php
$filename = "hits.txt" ;
$file = file($filename);
$file = array_unique($file);
$hits = count($file);
echo $hits;
$fd = fopen ($filename , "r");
$fstring = fread ($fd , filesize ($filename)) ;
fclose($fd) ;
$fd = fopen ($filename , "w");
$fcounted = $fstring."\n".getenv("REMOTE_ADDR");
$fout= fwrite ($fd , $fcounted );
fclose($fd);
?>
[/php]
[php]
<? include("count.txt"); ?>
[/php]
I am kind of new in PHP and I would like to add a counter on one simple page.
I copied this into my counter.php file:
<?php
if($count == ""){
setcookie("count", "m");
$file = file("count.txt");
$num = ($file[0] + 1);
exec("echo $num > count.txt");
}
?>
Then I created the count.txt file in the same folder as counter.php.
After trying to open view if counter.php works, I get the error: Parse error: syntax error, unexpected T_ECHO in C:\Webpage\counter.php on line 6.
Any ideas what's wrong?
Thanks for all your help!
<?php
if($_COOKIE == "")
{
setcookie("count", "m");
$file = file("count.txt");
$num = ($file[0] + 1);
echo $num;
}
?>
[/php]
LOL ... OK .. new error (they just keep on coming, don't they? :bawling: )
Parse error: syntax error, unexpected $end in C:\Webpage\counter.php on line 9.
Argghhh ...
/me reaches for his coffee and has a look more closely..
Paste your code as you have it please
Btw... errors are normal when you first start learning
My bad ...
It's working now. I overlooked something. Thanks for your help.
Not really .. actually it was a " missing.
Btw ... do you use anykind of "chat" programms ... like MSN or something .. you know ... if I would like some more help.
I am always logged into this site (except when I sleep and that ain't often) and will answer ya questions.
Threads about PHP promote interest from various people around here, so you can garner the responses of more than just one
I am planning to learn PHP but the counter was just something that I needed now.
If anybody has a great idea on how to learn programming in PHP .. I would really appreciate it. Are there "easy" ways of learning PHP? Is there a book or something like that. I know there is a manual on www.php.net ...
Well .. thanks for now.
You are all great!