View Full Version : Creating a PHP web Counter
imroue
11-18-2002, 06:55 PM
Hi,
I am trying to create a PHP counter, I think I know how to gather the information about the user, but I can't figure out what to do next. Let's say I have ther IP address, and the time and date.
How can I use that to save the information and display it on their screen?
any help would be appreciated..
thanks.
ConfusedOfLife
11-18-2002, 07:33 PM
Do you wana do that for all the viewers? If so, why do you wana save it at all?!
Anyway, here's a simple counter :
<?PHP
$fileName = "counter.txt";
# If you use "a+" or "a" in fopen, you can not fseek it
# backward! It puts the fp at the end of the file and
# you can not go back! Beware!!!
if ( file_exists( $fileName) )
$fp = fopen( getcwd() . '/' . $fileName, "r+");
else
$fp = fopen( getcwd() . '/' . $fileName, "w+");
$currNum = ( $oNum = fgets($fp) ) ? ( $oNum ) : ( 0 );
$currNum++;
# You can use fseek($fp, 0); insetead of rewind in here!
rewind($fp);
fwrite( $fp, $currNum);
fclose($fp);
echo $currNum;
?>
PS: You can use "print" or "echo" to output something, you can even write it as inline php, like:
<body>
<h1> <?php print "hello!" ?> <h1>
.
.
</body
or just simply :
<body>
<h1> <?= "hello!" ?> <h1>
.
.
</body
Note: You should check your php.ini file and make sure that support for short tags ( i.e. <? instead of <?php ) is on, I can't remember it's name at the moment!
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.