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.

PHP Code
  1. CREATE TABLE `guests` (
  2. `id` int(11) NOT NULL auto_increment,
  3. `remoteip` varchar(25) NOT NULL default '',
  4. `browser` varchar(50) NOT NULL default '',
  5. PRIMARY KEY (`id`)
  6. ) 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. //DB CONNECTION HERE
  3. //include your database connection script here ^^^
  4.  
  5. $array = get_browser(null, true); //get browser info into an array
  6. $agent = $array['browser']; //get only the browser name from the get_browser array
  7. $remoteip = $_SERVER['REMOTE_ADDR']; //get the guests ip.
  8.  
  9. $query = mysql_query("SELECT * FROM `guests` WHERE `remoteip` = '" . $remoteip . "'");
  10. //query for checking to see if the users visited before
  11.  
  12. if (mysql_num_rows($query) == 0){ //if they have not already visited the site/this page
  13. mysql_query("INSERT INTO `guests` (`remoteip`, `browser`) VALUES ('" . $remoteip . "', '" . $agent . "')"); //insert guest info into db
  14. //done!
  15. } //end of visited check
  16. ?>
  17. <!-- Just a html seperator ;) please read php code below and also remove this html comment ;) -->
  18. <?
  19. /*
  20. 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.
  21. WASIM ILYAS.
  22. */
  23. ?>
  24.  
  25. Guests:
  26. <?
  27. $guests = mysql_query("SELECT * FROM `guests`"); //make a query for all the guests in guest table
  28.  
  29. if (mysql_num_rows($guests) == "0"){ echo "No guests"; }else{ //if no guest records, echo appropriatley
  30. //otherwise do the stuff below
  31. ?>
  32. <table width=95% cellpadding="0" cellspacing="2" bgcolor="#CCCCCC">
  33. <tr>
  34. <td><center>Ip</center></td>
  35. <td><center>Browser</center></td>
  36. </tr>
  37. <? while($the = mysql_fetch_object($guests)){ //while loop for all records in $guests query ?>
  38. <tr>
  39. <td><center><? echo $the->remoteip; //echo remoteip column from record ?></center></td>
  40. <td><center><? echo $the->browser; //echo browser column from record ?></center></td>
  41. </tr>
  42. <? } //end of while loop ?>
  43. </table>
  44. <? } //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. $array = get_browser(null, true); //get browser info into an array
  2. $agent = $array['browser']; //get only the browser name from the get_browser array

with...
PHP Code
  1. $agent = $_SERVER['HTTP_USER_AGENT'];
  2. $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's Avatar
Views:
2,511
Rating:
Posted on Wednesday 25th June 2008 at 08:13 PM
schorsch
schorsch's Avatar
oki thx
Posted on Wednesday 25th June 2008 at 07:45 PM
ilyas-shezad
ilyas-shezad's Avatar
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's Avatar
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