
Originally Posted by
mburt
One of the very basic PHP counters (doesn't read I.P):
Code:
<?php
$cdata = file_get_contents("counter.txt");
$ndata = ($cdata+1);
$handle = fopen("counter.txt","w+");
fwrite($handle,$ndata);
fclose($handle);
echo "\n<br>".$cdata." visits";
?>
Wow, that's nifty. I never considered using a file.
To expand on that, here's how you would use sessions to only increase the count once per visit:
Code:
// do this stuff from here ...
session_start();
$cdata = file_get_contents("counter.txt");
$ndata = $cdata;
if (!isset($_SESSION['user_id']))
{
$_SESSION['user_id'] = 'user';
$ndata = ($cdata+1);
$handle = fopen("counter.txt","w+");
fwrite($handle,$ndata);
fclose($handle);
}
//... to here BEFORE sending any data to the browser.
echo "\n<br>".$ndata." visits";
Bookmarks