Forgot Password / Register
Site Statistics
Total Members: 509
Total Tutorials: 242
Newsest User: Lestop
Todays Unique Hits: 179
0 Users 2 Guests Online

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(nulltrue); //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
1
2
3
$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
1
2
3
$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
ilyas-shezad
Views:
1398
Rating:
Posted on Wednesday 25th June 2008 at 08:13 PM
schorsch
schorsch
oki thx
Posted on Wednesday 25th June 2008 at 07:45 PM
ilyas-shezad
ilyas-shezad
Yeh read towards the end of the tutorial where im takling about some servers not supporting the get_browser function so you need to follow the alternative i gave in tutorial.
Posted on Wednesday 25th June 2008 at 07:42 PM
schorsch
schorsch
Warning: get_browser() [function.get-browser]: browscap ini directive not set. in gstats.php on line 5



line5

$agent = $array['browser']; //get only the browser name from the get_browser array