PHP Classes

Smart Counter: Count site visitors using a MySQL database

Recommend this page to a friend!
  Info   View files Example   View files View files (2)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 1,680 All time: 2,343 This week: 101Up
Version License PHP version Categories
smart_counter 1.0.0GNU General Publi...4Databases, User Management


This class can be used to count the number of visitor of a site using a MySQL database table.

It can keep track of the total visitors of a site, visitors of a given page, visitors in the current day and the active visitors (access in the last 5 minutes).

The visits of users of the same IP address are only counted after ending a given period of time (20 minutes by default).

The visitis statistics are stored in two MySQL database tables with configurable names. The class can also create these tables if they do not exist already.

Picture of Vagharshak Tozalakyan
Name: Vagharshak Tozalakyan <contact>
Classes: 22 packages by
Country: United States United States
Age: 44
All time rank: 61 in United States United States
Week rank: 39 Down4 in United States United States Down
Innovation award
Innovation award
Nominee: 7x



// Include class definition

$sc = new smart_counter();
$sc->inc_interval = 1200; // 20 minutes

  // Update counter values with in-class autoconnection enabled
$sc->db_server = 'localhost';
$sc->db_username = 'root'; // the user must have permissions to create tables
$sc->db_password = '';
$sc->db_name = 'smart_counter'; // should exist
$sc->db_main_table = 'sc_main'; // will be created automatically
$sc->db_users_table = 'sc_users'; // will be created automatically

  Following code can be used if you want to connect the db by your own...

  $link_id = mysql_connect('localhost', 'root', '') or die(mysql_error());
  mysql_select_db('smart_counter') or die(mysql_error());

  $sc->db_name = 'smart_counter';
  $sc->db_main_table = 'sc_main';
  $sc->db_users_table = 'sc_users';
  $sc->db_link = $link_id;

echo sprintf('Total visits: %d<br />', $sc->get_total_visits());
sprintf('Visits on this page: %d<br />', $sc->get_page_visits());
sprintf('Today visits: %d<br />', $sc->get_today_visits());
sprintf('Online users: %d<br /><br />', $sc->get_active_visits(300));

$lv = $sc->get_latest_visitors();
  foreach (
$lv as $ip=>$info)
$ip . ' ';
$info['host'] . ' ';
date('F j, Y [H:i:s]', $info['time']) . ' ';
$info['location'] . '<br /><br />';


  Files folder image Files  
File Role Description
Plain text file smart_counter.class.php Class Class Source
Accessible without login Plain text file test.php Example Sample

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:2,343
This week:101Up