...

View Full Version : [Tutorial] Making A Hit Counter With PHP



stoodder
03-15-2003, 08:54 AM
okay you huys first i will give youthe code to analyze and then i will break it down into peices for oyu okay???


<?php
$filename = "hits.txt";
$fdata = fopen($filename, "r") or die ('Counter Error');
$fstring = fread($fdata, filesize($filename));
echo"$fstring";
fclose($fdata);

$fdata = fopen($filename, "w") or die ('Counter Error');
$fcounter = $fstring + 1;
$fend = fwrite($fd, $fcount);
fclose($fd);
?>


okay ill start by giving little definitions

$filename this is the exact fiole name of the file preferabbly .txt to write the counter surrent info to.

fopen fopen is a functiuon that goes and opens a file for later use in the script, the "r" stands for read it is going to read the file that it had just opend

fread mans that after the file is open it can read something within that file to get ready to display. in this case we are looking at the filesize of $filename

echo"$fstring"; this is what displays the counter... go ahead fiddle with it just make sure that iut has echo"$fstring OTHER TEXT HERE";

fclose this closes the current file notifying thet fread that that session it sone with the file

fopen with a "w" the "W" in this case stands for write, it is going to right the file when it is told to his means.

$fcounter this is the actually counter itsself.. this is what adds oin the number 1 to $fstring

$fend $fend is what finally sums up the proccess so that it writes to the file the current number plus on

then we do one mor efclose and you should bbe done! pretty easy? please email me if it isnt...stoodder@lycos.co.uk

pb&j
03-15-2003, 04:48 PM
I missed what the question was?

animyst
03-15-2003, 10:07 PM
Whats wrong with helping? It might stop people from posting questions when the answer is already here.

Spookster
03-15-2003, 10:16 PM
Well you might want to throw an flock on that file in case of simultaneous read/writes. You can also slim it down and only open the file once read the value increment it, echo it, then save it.

lord_raven
03-16-2003, 12:24 PM
Or do this instead:
<?php

$filename = "hits.txt";

$readarray = file ($filename);
$fstring = trim(implode ("", $readarray));
echo $fstring;

$fdata = fopen($filename, "w") or die ('Counter Error');
$fcounter = $fstring + 1;
$fend = fwrite($fdata, $fcount);
fclose($fd);

?>Edit: I forgot something important...

Spookster
03-16-2003, 05:57 PM
Originally posted by lord_raven
Or do this instead:
<?php

$filename = "hits.txt";

$readarray = file ($filename);
$fstring = trim(implode ("", $readarray));
echo $fstring;

$fdata = fopen($filename, "w") or die ('Counter Error');
$fcounter = $fstring + 1;
$fend = fwrite($fdata, $fcount);
fclose($fd);

?>Edit: I forgot something important...

you are still opening the file twice and a simulaneous read/write to the file can cause it to crash.

lord_raven
03-16-2003, 07:11 PM
Well, no matter what you do, unless you use MySQL, you're going to run into that problem...

Spookster
03-16-2003, 07:43 PM
Originally posted by lord_raven
Well, no matter what you do, unless you use MySQL, you're going to run into that problem...

Well, no. That's what flock is for. You lock the file before you read or write to it.

pb&j
03-17-2003, 08:29 PM
perhaps something like this?



<?php
$file = fopen($filename,"r") or die ('Counter Error');
flock ($file,2);
$counter = fread($file, filesize($filename));
$counter++;
flock($file,3);
fclose($file);

echo "You are visitor number $counter";

$file = fopen($filename,"w") or die ('Counter Error');
flock($file, 2);
$fend = fwrite($file, $counter);
flock($file, 3);
fclose($file);
?>

stoodder
03-17-2003, 10:34 PM
hmm can i ask you wghat the flock and fend functions do.. sounds like one ends it and one locks it but im nbot sure.. sorry im getting used to this still

lord_raven
03-17-2003, 10:42 PM
flock() sets the fopen() lock mode...

fend in this context is a variable called "constant"

If you were to put:
echo fend; #note: no quotes at the end, the output would be something to the affect of:

Resource id #3

pb&j
03-18-2003, 07:57 AM
Originally posted by stoodder
hmm can i ask you wghat the flock and fend functions do.. sounds like one ends it and one locks it but im nbot sure.. sorry im getting used to this still

flock will "lock" the file. that is temporarily lock access the file file while it is currently in use by another person. the 2 puts the lock on, the 3 takes it off.

i forgot the $ infront of the fend so i edited the post and you now can see it is simply a variable to be used to write the new data to the file.

stoodder
03-19-2003, 05:17 AM
hey thanx you guys for all of your help lol i feel like im asking to many questions i hope i dont get to annoying here i got one more question to ask but thats on the mysql dforum so yea thanx again both of you

hallj999
03-26-2003, 02:26 PM
how do you change the text and font coloue etc in this counter?

Nightfire
03-26-2003, 02:58 PM
echo "You are visitor number $counter";

Could be

echo "<font color=\"#ff0000\">$counter visitors have been here</font>";

hallj999
03-27-2003, 01:33 PM
how about the font its self

pb&j
03-27-2003, 02:44 PM
echo "<font color=\"#ff0000\" size=\"5\" face=\"FontNameHere\">$counter visitors have been here</font>";

(probably should use SPAN STYLE instead of FONT though considering its better standards)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum