Below is a simple example of creating a visitor counter with PHP. This implementation is very small and simple but it gives you a basic structure to work off of.
Visitor Counter
Simply writes a counter to 'HitCounter', or to the filename of your choice.
Code:
class HitCounter {
private $count;
/**
* Constructor.
*/
public function __construct($filename = 'HitCounter') {
$count = @file_get_contents($filename);
$count = $count ? $count : 0;
$fh = fopen($filename, 'w+');
fwrite($fh, ++$count);
fclose($fh);
$this->count = $count;
}
/**
* Get count.
*
* @return int
*/
public function get_count() {
return $this->count;
}
/**
* Output count.
*/
public function display_count() {
echo 'Hits: ' . $this->get_count();
}
}
Counter Usage
Output the counter directly to the page.
Code:
$counter = new HitCounter();
$counter->display_count();
Custom counter message.
$counter = new HitCounter();
$count = $counter->get_count();
echo 'The count is currently ' . $count;
If you liked my tutorial please say thanks, i use a more advanced one of this but this still works, if you have some trouble with it let me know and i'll fix it. -.^