Geekpedia Programming Tutorials






Getting visitor information

Getting information about your visitors is interesting and sometimes useful for generating statistics, especially when you have a high traffic.

On Tuesday, March 23rd 2004 at 04:46 PM
By Andrew Pociu (View Profile)
*****   (Rated 4.2 with 17 votes)
Contextual Ads
More PHP Resources
Advertisement

With PHP it's easy to get your visitor's IP, browser and operating system information, the page that referred him, the URL he visited and the time when he did it.
For this we use the following predefined variables:

$_SERVER['REMOTE_ADDR']

- gets the visitor's IP

Ex.: 192.168.0.1 (localhost IP)



$_SERVER['HTTP_USER_AGENT']

- the visitor's browser and operating system information.

Ex.: Mozilla/4.0 (compatible; MSIE 6.0; Windows 98)
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Crazy Browser 1.0.5; .NET CLR)
Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
Mozilla/4.0 (compatible; MSIE 6.0; Windows 98; Hotbar 4.4.2.0)
Googlebot/2.1 (+http://www.googlebot.com/bot.html) - it means that Google crawls your website.

Mozilla/4.0 (compatible; MSIE 6.0... - this means the browser is Internet Explorer 6.0
Windows NT 5.1;... - this means the operating system is Microsoft Windows XP
Hotbar 4.4.2.0 - this is a browser extension (add-on)


$_SERVER['HTTP_ACCEPT_LANGUAGE']

- the visitor's operating system language.

Ex.: en, fr, hr, zh

You can find information about language codes at http://www.mondotimes.com/about/pop/languages.html.


$_SERVER['HTTP_REFERER']

- the URL that referred the visitor to the current address.

Ex.: http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/
/prog_ttrls_by_categ.php?id=4&order=date&sort=desc&items=10&start=0

If the referer is internal (a page in your website, the referer will appear as in the second example. This way you can track your visitors closely.


$_SERVER['REQUEST_URI']

- the current URL the visitor has in its browser.

Ex.: /soft_artcls_list.php
/prj_proj_by_categ.php?id=3&order=date&sort=desc&items=10&start=0
/

/ - means the root, the main page (index.php, index.html, default.asp...), for example www.geekpedia.com.



This is the code you'll insert in your header that is probably included in all your pages.



mysql_query("insert into stats(vis_ip, vis_agent, vis_lang, vis_ref, vis_time, vis_url, vis_author, vis_country) values('".$_SERVER['REMOTE_ADDR']."', '".$_SERVER['HTTP_USER_AGENT']."', '".$_SERVER['HTTP_ACCEPT_LANGUAGE']."', '".$_SERVER['HTTP_REFERER']."', NOW(), '".$_SERVER['REQUEST_URI']."')");




And this is the query for the database:



CREATE TABLE `stats` (
  `id_stat` bigint(5) NOT NULL auto_increment,
  `vis_ip` varchar(15) NOT NULL default '',
  `vis_agent` varchar(80) NOT NULL default '',
  `vis_lang` char(2) NOT NULL default '',
  `vis_ref` varchar(192) NOT NULL default '',
  `vis_time` datetime NOT NULL default '0000-00-00 00:00:00',
  `vis_url` varchar(96) NOT NULL default '',
  PRIMARY KEY  (`id_stat`)
) TYPE=MyISAM AUTO_INCREMENT=1720 ;


For more information, perhaps the visitor's country you need an IP2Country database. This will probably pe covered in another tutorial .
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by Bogdan Hurezeranu on Saturday, May 8th 2004 at 06:04 PM

nice tutorial

by bs0d on Tuesday, May 11th 2004 at 05:56 PM

I think thats a good tutorial for webmasters wanting to keep their own statistics of who is visiting their site.

A+ Andrei

by Seldimi on Friday, December 10th 2004 at 08:59 AM

Very useful to include in some scripts for security reasons :)
As for stats, a graph would be more preferable (hope to be coverd in another tutorial)

by Sreejith on Wednesday, August 31st 2005 at 02:29 PM

Nice tutorial :)

by Masoud on Sunday, September 24th 2006 at 08:38 AM

i look out all return $_SERVER['HTTP_USER_AGENT'] value
for find browser name and ver and os with ver

by bluefrogx on Thursday, October 25th 2007 at 06:47 PM

Bad idea to dump the http referer directly into the database. What's to stop me from faking a referal that includes ' in it. I could easily inject SQL To the database that way.

Excellent attempt though, it just isn't too secure.

by Andrei Pociu on Thursday, October 25th 2007 at 08:06 PM

The default PHP server configuration will automatically escape the quotes. But if you feel more secure (or using an old version of PHP) you can always use addslashes(), htmlspecialchars() to strip down the strings.

by Bill Hosinfluk on Friday, December 21st 2007 at 08:45 PM

@ bluefrogx why don't you just escape the '

by thiha on Thursday, September 18th 2008 at 05:50 AM

this tutorial express how can i know other's online visitor's ip

by waseem on Sunday, August 9th 2009 at 03:27 AM

cooooollllll

by dinesh on Thursday, September 17th 2009 at 08:30 AM

Thanks Dear you have solved my problem

<a href="http://www.solutions4ever.net">ASP.NET</a>


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs PHP Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Latest Tech Bargains

Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons