• home
  • forum
  • my
  • kt
  • download
  • PHP/mySQL Simple User Counter

    Author: 2008-08-16 08:34:55 From:

    Today I am going to show you how to make a simple user counter with total and unique hit statistics. This is just a simple script tutorials so you will be able to build upon it.

    Let set up our database structure:

    1. <?php
    2. // This is how your DB will be setup.
    3. /*
    4. CREATE TABLE IF NOT EXISTS `visitors` (
    5.   `visitor_id` int(11) NOT NULL auto_increment,
    6.   `name` varchar(225) NOT NULL default ”,
    7.   `value` varchar(225) NOT NULL default ”,
    8.   PRIMARY KEY  (`visitor_id`)
    9. ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
    10.  
    11. INSERT INTO `visitors` (`visitor_id`, `name`, `value`) VALUES
    12. (1, ‘ip’, ”),
    13. (2, ‘hits’, ‘0,0′);
    14. */
    15. ?>

    Now to call the database in conf.inc.php:

    1. <?php
    2. $db_user = ""; // Username
    3. $db_pass = ""; // Password
    4. $db_database = ""; // Database Name
    5. $db_host = ""; // Server Hostname
    6. $db_connect = mysql_connect ($db_host, $db_user, $db_pass);
    7. $db_select = mysql_select_db ($db_database);
    8.  
    9. // Code to pull the results from the db.
    10. $q = mysql_query("SELECT * FROM `visitors` WHERE `name` = ‘hits’"); // Finds the row with the numbers
    11. $visitor = mysql_fetch_array($q);
    12. $visitor = split(",", $visitor[‘value’]); // Splits the query result into an array.
    13. define(‘SITE_HITS’, $visitor[‘0′]); //  Defines a constant for all hits
    14. define(‘SITE_UNI_HITS’, $visitor[‘1′]); // Defines a constant for unique hits
    15.  
    16. function visitor($ip) { // Function that will be put on main page.
    17.         $q_ip = mysql_query("SELECT * FROM `visitors` WHERE `name` = ‘ip’") or die(mysql_error()); // Finds the info for all of the ip’s
    18.         $r_ip = mysql_fetch_array($q_ip); // Fetches the ip array we will use later.
    19.         $array_ip = split("-", $r_ip[‘value’]); // Splits the results and turns it into an array of all the ips
    20.        
    21.         $q_hit = mysql_query("SELECT * FROM `visitors` WHERE `name` = ‘hits’") or die(mysql_error()); // Finds the info for the hits
    22.         $r_hit = mysql_fetch_array($q_hit);
    23.         $hit = split(",", $r_hit[‘value’]); // Splits the query results into an array.
    24.         $hits = $hit[‘0′]; // The total hits
    25.         $uni = $hit[‘1′]; // The unique hits
    26.        
    27.         if (in_array($ip, $array_ip)) { // If the users ip is in the array we defined earlier
    28.                 $hits++; // Adds a hit, but not a unique hit
    29.         } else {
    30.                 $hits++; // Adds both a unique and hit because he is a new user.
    31.                 $uni++;
    32.                 $ip = $r_ip[‘value’] . $ip; // Adds the new ip to the ips already in the db
    33.                 mysql_query("UPDATE `visitors` SET `value` = ‘$ip’ WHERE `name` = ‘ip’"); // Updates the table
    34.         }
    35.        
    36.         $hits = $hits . "," . $uni; // Combines the hits and unique hits
    37.         mysql_query("UPDATE `visitors` SET `value` = ‘$hits’ WHERE `name` = ‘hits’"); // Updates the table
    38. }
    39. ?>

    Breakdown:
    First we set the db info.
    Next we define the hit/unique constants to display it on the stats page.
    After we create the function visitors to add the users ip and hits into the db.

    Now the code you would put in the index.php or the page you want the stats for.

    1. <?php
    2. include("conf.inc.php");
    3. visitor($_SERVER["REMOTE_ADDR"]); // Use the function we defined earlier with the users ip.
    4. echo "Main page";
    5. ?>

    Breakdown:
    First we include the conf.inc.php for the db info and the functions.
    Next we initiate the function visitors with the users ip.

    Now for the stats.php page:

    1. <?php
    2. include("conf.inc.php");
    3. echo "Total Hits: " . SITE_HITS;
    4. echo "
    5. ";
    6. echo "Unique Hits: " . SITE_UNI_HITS;
    7. ?>

    And thats it, I hope you build off it. If you have any questions please feel free to post. Thanks

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (8)
      Counters (8)
      Database Related (8)
      Date and Time (9)
      Development (6)
      Discussion Boards (7)
      E Commerce (6)
      Email Systems (9)
      Error Handling (5)
      File Manipulation (10)
      Flash and PHP (4)
      Form Processing (7)
      Guestbooks (8)
      Image Manipulation (3)
      Installing PHP (5)
      Introduction to PHP (9)
      Link Indexing (6)
      Mailing List Management (8)
      Miscellaneous (10)
      Networking (6)
      News Publishing (6)
      OOP (8)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (5)
      Postcards (0)
      Randomizing (8)
      Redirection (8)
      Searching (6)
      Security (6)
      Site Navigation (7)
      User Authentication (10)
      WAP and WML (7)
      Web Fetching (0)
      Web Traffic Analysis (14)
      XML and PHP (16)

    New

    Hot