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
StarStarStar 58%Total: 1,682 This week: 1All time: 2,342 This week: 560Up
Version License PHP version Categories
smart_counter 1.0.0GNU General Publi...4Databases, User Management
Description 

Author

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: 411 Down48 in United States United States Down
Innovation award
Innovation award
Nominee: 7x

Example

<?php

 
// Include class definition
 
require_once('smart_counter.class.php');

 
$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
 
$sc->update_counter();

/*
  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;
  $sc->update_counter(FALSE);
*/

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

 
$lv = $sc->get_latest_visitors();
  foreach (
$lv as $ip=>$info)
  {
    echo
$ip . ' ';
    echo
$info['host'] . ' ';
    echo
date('F j, Y [H:i:s]', $info['time']) . ' ';
    echo
$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  
 0%
Total:1,682
This week:1
All time:2,342
This week:560Up
 User Ratings  
 
 All time
Utility:83%StarStarStarStarStar
Consistency:83%StarStarStarStarStar
Documentation:-
Examples:83%StarStarStarStarStar
Tests:-
Videos:-
Overall:58%StarStarStar
Rank:1431