PHP-mySQL Simple User Counter by ciccone85


More Info
									                  PHP/mySQL Simple User Counter
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:

// This is how your DB will be setup.
   `visitor_id` int(11) NOT NULL auto_increment,
   `name` varchar(225) NOT NULL default '',
   `value` varchar(225) NOT NULL default '',
   PRIMARY KEY (`visitor_id`)

INSERT INTO `visitors` (`visitor_id`, `name`, `value`) VALUES
(1, 'ip', ''),
(2, 'hits', '0,0');

Now to call the database in

$db_user = ""; // Username
$db_pass = ""; // Password
$db_database = ""; // Database Name
$db_host = ""; // Server Hostname
$db_connect = mysql_connect ($db_host, $db_user, $db_pass);
$db_select = mysql_select_db ($db_database);

// Code to pull the results from the db.
$q = mysql_query("SELECT * FROM `visitors` WHERE `name` = 'hits'"); //
Finds the row with the numbers
$visitor = mysql_fetch_array($q);
$visitor = split(",", $visitor['value']); // Splits the query result
into an array.
define('SITE_HITS', $visitor['0']); // Defines a constant for all hits
define('SITE_UNI_HITS', $visitor['1']); // Defines a constant for
unique hits

function visitor($ip) { // Function that will be put on main page.
        $q_ip = mysql_query("SELECT * FROM `visitors` WHERE `name` =
'ip'") or die(mysql_error()); // Finds the info for all of the ip's
        $r_ip = mysql_fetch_array($q_ip); // Fetches the ip array we
will use later.
        $array_ip = split("-", $r_ip['value']); // Splits the results
and turns it into an array of all the ips

        $q_hit = mysql_query("SELECT * FROM `visitors` WHERE `name` =
'hits'") or die(mysql_error()); // Finds the info for the hits
        $r_hit = mysql_fetch_array($q_hit);
        $hit = split(",", $r_hit['value']); // Splits the query results
into an array.
        $hits = $hit['0']; // The total hits
        $uni = $hit['1']; // The unique hits

        if (in_array($ip, $array_ip)) { // If the users ip is in the
array we defined earlier
                $hits++; // Adds a hit, but not a unique hit
        } else {
                $hits++; // Adds both a unique and hit because he is a
new user.
                $ip = $r_ip['value'] . $ip; // Adds the new ip to the
ips already in the db
                mysql_query("UPDATE `visitors` SET `value` = '$ip' WHERE
`name` = 'ip'"); // Updates the table

        $hits = $hits . "," . $uni; // Combines the hits and unique hits
        mysql_query("UPDATE `visitors` SET `value` = '$hits' WHERE
`name` = 'hits'"); // Updates the table

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.

visitor($_SERVER["REMOTE_ADDR"]); // Use the function we defined
earlier with the users ip.
echo "Main page";

First we include the for the db info and the functions.
Next we initiate the function visitors with the users ip.

Now for the stats.php page:
echo "Total Hits: " . SITE_HITS;
echo "<br />";
echo "Unique Hits: " . SITE_UNI_HITS;

Have fun!

To top