Unique Visitor Counter in PHP

  • 60 Unique Visitors
  • 181 Page Views

Congratulations! You are lucky visitor number... 61!!!

61 visitors

182 views

  • 289,808 Unique Visitors
  • 384,305 Page Views

Similar to the PHP Pageview Counter, this will display the number of unique visitors to your site, styled however you like.

Set a cookie in PHP or JavaScript so that the same visitor isn't counted twice until a designated amount of time.

This script includes the same extra features listed on the PHP Pageview Counter page.

Source Code

Note: This is not the same source code used in production. This is just a working starting point.

PHP

$visitor_txt = 'visitors.txt';

$dat = file_get_contents($visitor_txt);
if (!isset($_COOKIE[$cookie_name])) {
if ($dat !== false)
{
$fil = fopen($visitor_txt, "w");
if (flock($fil, LOCK_EX)) {
echo '',number_format($dat2+1),' unique visitors';
fwrite($fil, $dat+1);
flock($fil, LOCK_UN);
}
fclose($fil);
}
}

else {
echo '',number_format($dat+0),' unique visitors';
}