Jun 28

Coming Soon…

Posted by Pat

I am leaving for Italy on a cruise this weekend. I will be gone until the following weekend. I have reviewed all of the comments I have received and when I come back, I will post the following tutorials:

1) PHP Poll System
2) PHP Gallery System
3) Securing PHP scripts

And possibly if I have time, expand on the membership system.

Post any other requests you might have :D

Jun 5

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
$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.
		$uni++;
		$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
}
?>

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

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
2
3
4
5
6
<?php
include("conf.inc.php");
echo "Total Hits: " . SITE_HITS;
echo "<br />";
echo "Unique Hits: " . SITE_UNI_HITS;
?>

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