Guest Statistics
Hey, made this tutorial in a rush and my server was working against me on this one since it didnt support one of the functions in this tutorial...
Anyway lets begin shall we?
First off, we need to make a table on which we can store all of our guest information.
Code
CREATE TABLE `guests` (
`id` int(11) NOT NULL auto_increment,
`remoteip` varchar(25) NOT NULL default '',
`browser` varchar(50) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3;
The table is called
guests and has 3 fields. The first is the id (will be inserted in numeric order), the second is the remote ip (which is basically the persons IP address) and the third is that users browser.
Now its time to make all the code for the thing. Im going to put this all on one page. My code is commented. Lets call this page
guest-stats.php shall we?
guest-stats.php code:
PHP Code
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
40
41
42
43
44
<?
//DB CONNECTION HERE
//include your database connection script here ^^^
$array = get_browser(null, true); //get browser info into an array
$agent = $array['browser']; //get only the browser name from the get_browser array
$remoteip = $_SERVER['REMOTE_ADDR']; //get the guests ip.
$query = mysql_query("SELECT * FROM `guests` WHERE `remoteip` = '" . $remoteip . "'");
//query for checking to see if the users visited before
if (mysql_num_rows($query) == 0){ //if they have not already visited the site/this page
mysql_query("INSERT INTO `guests` (`remoteip`, `browser`) VALUES ('" . $remoteip . "', '" . $agent . "')"); //insert guest info into db
//done!
} //end of visited check
?>
<!-- Just a html seperator ;) please read php code below and also remove this html comment ;) -->
<?
/*
PLEASE NOTE! ALL THE BELOW CODE CAN BE PUT ON ANOTHER PAGE. DOES NOT NESSESARILY NEED TO BE ON THIS PAGE. HOWEVER WHEREVER YOU PUT IT YOU MUST REMEMBER TO INCLUDE YOUR DATABASE CONNECTION SCRIPT.
WASIM ILYAS.
*/
?>
Guests:
<?
$guests = mysql_query("SELECT * FROM `guests`"); //make a query for all the guests in guest table
if (mysql_num_rows($guests) == "0"){ echo "No guests"; }else{ //if no guest records, echo appropriatley
//otherwise do the stuff below
?>
<table width=95% cellpadding="0" cellspacing="2" bgcolor="#CCCCCC">
<tr>
<td><center>Ip</center></td>
<td><center>Browser</center></td>
</tr>
<? while($the = mysql_fetch_object($guests)){ //while loop for all records in $guests query ?>
<tr>
<td><center><? echo $the->remoteip; //echo remoteip column from record ?></center></td>
<td><center><? echo $the->browser; //echo browser column from record ?></center></td>
</tr>
<? } //end of while loop ?>
</table>
<? } //end of guest records check ?>
I have read that a lot of servers don't support the function
get_browser which is kinda a problem.... but there is an alternative :D
If your server does not support the get_browser() function then replace...
PHP Code
$array = get_browser(null, true); //get browser info into an array
$agent = $array['browser']; //get only the browser name from the get_browser array
with...
PHP Code
$agent = $_SERVER['HTTP_USER_AGENT'];
$agent = substr($agent, 0, 15);
This string will be long, ugly and sometimes even incorrect. Sorry but i could'nt find any other alternative!
Anyway feel free to drop a comment.
Thanks