<?php
/*
-=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=-
Name:
gnVisitorInfo Class
Author:
Girish Nair <hide@address.com>
Description:
This class tries to get as much as information about a visitor to your
site. like the country,proxy,user agent informations.
Terms:
Copyright (C) 2003-2004 Girish Nair
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details at:
http://www.gnu.org/copyleft/lesser.html
If you use this script in your application/website, please
send me an e-mail letting me know about it :)
Bugs:
Please report any bugs you might find to my e-mail address
at (Girish Nair <hide@address.com>) [remove all the '_'
characters]. If you have a fix/patch for the bug, please do send
it to me so I can incorporate it into the next release.
Suggestion:
I know there is a lot of scope for improvement! if you find any
suggestion useful for this program please send it to my e-mail address
at (Girish Nair <hide@address.com>).
Donation:
If you found this class useful. and if you want to give something in
return then please donate Rs. 50/- (OR 1USD )to a charity organization
nearby. And let my name also be there along with yours :)
And remember to sign my guestbook @ www.girishn.com
Version History:
0.1 17 October, 2003 - InitialRelease.
0.2 04 June, 2004 - Some minor fixes/Enhancements.
Extracting Browser & OS related
details.
-=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=-
*/
require_once('db_mysql.inc');
class gnVisitorInfo {
var $debug;
var $serverVars;
var $db;
var $IP;
var $RequestPage;
var $URL;
var $RequestMethod;
var $UA;
var $Broswer;
var $BroswerVersion;
var $OS;
var $Referer;
var $C2Code;
var $C3Code;
var $CountryName;
var $ProxyUsed;
var $ProxyIP;
var $ProxyCountry;
var $ProxySignature;
var $AcceptedLanguage;
var $AcceptedEncoding;
var $AcceptedCharset;
/**
* Prints the debug message
* @param $msg the debug message to print
*
*/
function debug($msg) {
echo ($this->debug==1) ? "\n<br>Debug: $msg": "";
}
/**
* Prints the array contents
* @param $array the array to print
*
*/
function debug_r($array) {
if($this->debug==1) {
echo "<pre>";
print_r($array);
echo "</pre>";
}
}
/**
* Constructor
* @param $totalItems total number of items to show, (it is not the total no of pages!)
* @param $itemsPerPage no of items shown per page
*/
function gnVisitorInfo() {
$this->gnSetVInfoServerVars($_SERVER);
$this->debug=0;
$this->db= new DB_sql;
}
function gnSetVInfoServerVars($serverVars) {
$this->serverVars=$serverVars;
}
function gnGetVInfoIP() {
//return the IP
return $this->IP;
}
function gnGetVInfoRequestPage () {
//return the REQUEST_URI
return $this->RequestPage;
}
function gnGetVInfoURL() {
//return the actual url(SCRIPT_NAME,QUERY_STRING)
return $this->URL;
}
function gnGetVInfoRequestMethod() {
//return the REQUEST_METHOD
return $this->RequestMethod;
}
function gnGetVInfoUA() {
//return the user agent sign
return $this->UA;
}
function gnGetVInfoBrowser() {
//return the Browser Name
return $this->Broswer;
}
function gnGetVInfoBrowserVersion() {
//return the Browser Name
return $this->BroswerVersion;
}
function gnGetVInfoOS() {
//return the Operating system
return $this->OS;
}
function gnGetVInfoReferer() {
//return the HTTP_REFERER
return $this->Referer;
}
function gnGetVInfoCountry() {
//return the country code
return array($this->C2Code,$this->C3Code,$this->CountryName);
}
function gnGetVInfoProxyUsed() {
//return (true,false) whether proxy used or not
return $this->ProxyUsed;
}
function gnGetVInfoProxyIP() {
//return the IP's of proxies found
return $this->ProxyIP;
}
function gnGetVInfoProxyCountry() {
//return the Country detailsof proxies found
return $this->ProxyCountry;
}
function gnGetVInfoProxySignature() {
//return the Signatures of Proxy servers.
return $this->ProxySignature;
}
function gnGetVInfoAcceptedLanguage() {
//return the User Accepted Languages.
return $this->AcceptedLanguage;
}
function gnGetVInfoAcceptedEncoding() {
//return the User Accepted Encoding.
return $this->AcceptedEncoding;
}
function gnGetVInfoAcceptedCharset() {
//return the User Accepted Charset.
return $this->AcceptedCharset;
}
function gnGetVInfo () {
//return all the info we have.
$ret_arr['IP'] = $this->gnGetVInfoIP();
$ret_arr['RequestPage'] = $this->gnGetVInfoRequestPage ();
$ret_arr['URL'] = $this->gnGetVInfoURL();
$ret_arr['RequestMethod'] = $this->gnGetVInfoRequestMethod();
$ret_arr['UA'] = $this->gnGetVInfoUA();
$ret_arr['Browser'] = $this->gnGetVInfoBrowser();
$ret_arr['BrowserVersion'] = $this->gnGetVInfoBrowserVersion();
$ret_arr['OS'] = $this->gnGetVInfoOS();
$ret_arr['Referer'] = $this->gnGetVInfoReferer();
$ret_arr['AcceptedLanguage'] = $this->gnGetVInfoAcceptedLanguage();
$ret_arr['AcceptedEncoding'] = $this->gnGetVInfoAcceptedEncoding();
$ret_arr['AcceptedCharset'] = $this->gnGetVInfoAcceptedCharset();
$ret_arr['Country'] = $this->gnGetVInfoCountry();
if($this->gnGetVInfoProxyUsed()==1) {
$ProxyIP = $this->gnGetVInfoProxyIP();
$ProxyCountry = $this->gnGetVInfoProxyCountry();
$ProxySignature = $this->gnGetVInfoProxySignature();
foreach($ProxyIP as $k=>$pIP) {
$ret_arr['ProxyDetail'][]=array('IP'=>$pIP,'Signature'=>$ProxySignature[$k],'Country'=>$ProxyCountry[$k]);
}
}
return $ret_arr;
}
///// now the functions for Extracting gnVisitorInfo.
function gnExtractVInfoIP() {
//return the IP
$this->IP=$this->serverVars['REMOTE_ADDR'];
$this->debug('Extracted IP:'.$this->IP);
}
function gnExtractVInfoRequestPage () {
//return the REQUEST_URI
$this->RequestPage=$this->serverVars['REQUEST_URI'];
$this->debug('Extracted RequestPage:'.$this->RequestPage);
}
function gnExtractVInfoURL() {
//return the actual url(SCRIPT_NAME,QUERY_STRING)
$this->URL=$this->serverVars['SCRIPT_NAME'].'?'.$this->serverVars['QUERY_STRING'];
$this->debug('Extracted URL:'.$this->URL);
}
function gnExtractVInfoRequestMethod() {
//return the request method (get,post ...)
$this->RequestMethod=$this->serverVars['REQUEST_METHOD'];
$this->debug('Extracted RequestMethod:'.$this->RequestMethod);
}
function gnExtractVInfoUA() {
//return the user agent
$this->UA=$this->serverVars['HTTP_USER_AGENT'];
$this->debug('Extracted UA:'.$this->UA);
}
function gnExtractVInfoBrowser() {
//return the user agent
$this->Broswer='Not Detected';
$this->BroswerVersion='';
$this->gnExtractVInfoUA();
$userAgentStr=$this->UA;
if(($pos = strpos($userAgentStr,"MSIE")) !== false) {
$this->Broswer='Internet Explorer';
$pos+=strlen("MSIE")+1;
$endPos=strpos($userAgentStr,";",$pos);
$endPos = ($endPos===false) ? strlen($userAgentStr) : $endPos-$pos;
$this->BroswerVersion = substr($userAgentStr,$pos,$endPos);
} else if(($pos = strpos($userAgentStr,"Mozilla Firebird")) !== false) {
$this->Broswer='Mozilla Firebird';
$pos+=strlen("Mozilla Firebird")+1;
$endPos=strpos($userAgentStr,";",$pos);
$endPos = ($endPos===false) ? strlen($userAgentStr) : $endPos-$pos;
$this->BroswerVersion = substr($userAgentStr,$pos,$endPos);
} else if(($pos = strpos($userAgentStr,"Netscape")) !== false) {
$this->Broswer='Netscape Navigator';
$pos+=strlen("Netscape")+1;
$endPos=strpos($userAgentStr," ",$pos);
$endPos = ($endPos===false) ? strlen($userAgentStr) : $endPos-$pos;
$this->BroswerVersion = substr($userAgentStr,$pos,$endPos);
} else if(($pos = strpos($userAgentStr,"Mozilla")) !== false) {
$this->Broswer='Mozilla';
$pos+=strlen("Mozilla")+1;
$endPos=strpos($userAgentStr," ",$pos);
$endPos = ($endPos===false) ? strlen($userAgentStr) : $endPos-$pos;
$this->BroswerVersion = substr($userAgentStr,$pos,$endPos);
}
$this->debug('Extracted Browser:'.$this->Broswer . ' - '.$this->BroswerVersion);
}
function gnExtractVInfoOS() {
//return the user agent
$this->gnExtractVInfoUA();
$this->OS='Not Detected';
$userAgentStr=$this->UA;
if(eregi("Windows",$userAgentStr)) {
/* $pos=strpos($userAgentStr,"Windows");
$endPos=strpos($userAgentStr,";",$pos);
$endPos=($endPos>0) ? $endPos: strpos($userAgentStr,")",$pos);
echo '--------'.substr($userAgentStr,$pos,($endPos-$pos));
*/
$this->OS='Windows';
} else if(eregi("Linux",$userAgentStr)) {
$this->OS='Linux';
} else if(eregi("Unix",$userAgentStr)) {
$this->OS='Unix';
} else if(eregi("Mac",$userAgentStr)) {
$this->OS='Macintosh';
} else if(eregi("SunOS",$userAgentStr)) {
$this->OS='SunOS';
} else if(eregi("HP-UX",$userAgentStr)) {
$this->OS='Unix';
} else if(eregi("IRIX",$userAgentStr)) {
$this->OS='Unix';
}
$this->debug('Extracted OS:'.$this->OS);
}
function gnExtractVInfoReferer() {
//return the HTTP_REFERER
$this->Referer=$this->serverVars['HTTP_REFERER'];
$this->debug('Extracted Referer:'.$this->Referer);
}
function gnExtractVInfoCountry() {
$serverDetail=$this->gnServerDetails($this->serverVars['REMOTE_ADDR']);
$this->C2Code =$serverDetail['c2code'];
$this->C3Code =$serverDetail['c3code'];
$this->CountryName =$serverDetail['country'];
$this->debug('Extracted Country:'.$this->C2Code.':'.$this->C3Code.':'.$this->CountryName);
}
function gnExtractVInfoProxyUsed() {
//return (true,false) whether proxy used or not
$this->ProxyUsed= ($this->serverVars['HTTP_VIA'] || $this->serverVars['HTTP_X_FORWARDED_FOR'] );
$this->debug('Extracted ProxyUsed:'.$this->ProxyUsed);
}
function gnExtractVInfoProxyIP() {
//return the IP's of proxies found
if($this->ProxyUsed) {
$proxyArr=explode(",",$this->serverVars['HTTP_X_FORWARDED_FOR']);
if(count($proxyArr)>1) {
$this->ProxyIPArr=1; //// don't know why this is required, will be utilised/removed by next release
$this->ProxyIP=$proxyArr;
$this->debug('Extracted ProxyIP:'.$this->ProxyIPArr.':');
$this->debug_r($this->ProxyIP);
foreach($proxyArr as $k=>$server) {
$serverDetail=$this->gnServerDetails($server);
$this->ProxyCountry[$k]['C2Code'] =$serverDetail['c2code'];
$this->ProxyCountry[$k]['C3Code'] =$serverDetail['c3code'];
$this->ProxyCountry[$k]['CountryName'] =$serverDetail['country'];
}
} else {
$this->ProxyIPArr=0; //// don't know why this is required, will be utilised/removed by next release
$this->ProxyIP=array($proxyArr[0]);
$this->debug('Extracted ProxyIP:'.$this->ProxyIPArr.':'.$this->ProxyIP);
$serverDetail=$this->gnServerDetails($proxyArr[0]);
$this->ProxyCountry[0]['C2Code'] =$serverDetail['c2code'];
$this->ProxyCountry[0]['C3Code'] =$serverDetail['c3code'];
$this->ProxyCountry[0]['CountryName'] =$serverDetail['country'];
}
$this->debug('Extracted Proxy Country:');
$this->debug_r($this->ProxyCountry);
}
}
function gnExtractVInfoProxySignature() {
//return the Signatures of Proxy servers.
if($this->ProxyUsed) {
$proxyArr=explode(",",$this->serverVars['HTTP_VIA']);
if(count($proxyArr)>1) {
$this->ProxySignatureArr=1; //// don't know why this is required, will be utilised/removed by next release
$this->ProxySignature=$proxyArr;
$this->debug('Extracted ProxySignature:'.$this->ProxySignatureArr.':');
$this->debug_r($this->ProxySignature);
} else {
$this->ProxySignatureArr=0; //// don't know why this is required, will be utilised/removed by next release
$this->ProxySignature=array($proxyArr[0]);
$this->debug('Extracted ProxySignature:'.$this->ProxySignatureArr.':'.$this->ProxySignature);
}
}
}
function gnExtractVInfoAcceptedLanguage() {
//return the Accepted Languages
if(isset($this->serverVars['HTTP_ACCEPT_LANGUAGE']) && !empty($this->serverVars['HTTP_ACCEPT_LANGUAGE'])) {
$lArr=explode(',',$this->serverVars['HTTP_ACCEPT_LANGUAGE']);
foreach($lArr as $k => $v) {
$vArr=explode(";",$v);
$vArr[1]=(!empty($vArr[1])) ? str_replace("q=","",$vArr[1]) : 1.0;
$tmplCArr[$vArr[0]]=$vArr[1];
}
$lDetailsArr=$this->gnLangDetails(array_keys($tmplCArr));
foreach($tmplCArr as $lC=>$qV) {
$langArr[]=array("langCode"=>$lC,"langName"=>$lDetailsArr[$lC],"langQValue"=>$qV);
}
} else {
$langArr[]=array("langCode"=>'',"langName"=>"all languages are equally acceptable","langQValue"=>1.0);
}
$this->AcceptedLanguage=$langArr;
$this->debug('Extracted Accepted Language:');
$this->debug_r($this->AcceptedLanguage);
}
function gnExtractVInfoAcceptedEncoding() {
//return the Accepted Encoding
if(isset($this->serverVars['HTTP_ACCEPT_ENCODING']) && !empty($this->serverVars['HTTP_ACCEPT_ENCODING'])) {
$eArr=explode(',',$this->serverVars['HTTP_ACCEPT_ENCODING']);
foreach($eArr as $k => $v) {
$vArr=explode(";",$v);
$vArr[1]=(!empty($vArr[1])) ? str_replace("q=","",$vArr[1]) : 1.0;
$encArr[]=array("encoding"=>$vArr[0],"encodingQValue"=>$vArr[1]);
}
} else {
$encArr[]=array("encoding"=>'',"encodingQValue"=>1.0);
}
$this->AcceptedEncoding=$encArr;
$this->debug('Extracted Accepted Encoding:');
$this->debug_r($this->AcceptedEncoding);
}
function gnExtractVInfoAcceptedCharset() {
//return the Accepted Charset
if(isset($this->serverVars['HTTP_ACCEPT_CHARSET']) && !empty($this->serverVars['HTTP_ACCEPT_CHARSET'])) {
$cArr=explode(',',$this->serverVars['HTTP_ACCEPT_CHARSET']);
foreach($cArr as $k => $v) {
$vArr=explode(";",$v);
$vArr[1]=(!empty($vArr[1])) ? str_replace("q=","",$vArr[1]) : 1.0;
$charsetArr[]=array("charset"=>$vArr[0],"charsetQValue"=>$vArr[1]);
}
} else {
$charsetArr[]=array("charset"=>'',"charsetQValue"=>1.0);
}
$this->AcceptedCharset=$charsetArr;
$this->debug('Extracted Accepted Charset:');
$this->debug_r($this->AcceptedCharset);
}
function gnExtractVInfo () {
//return all the info we have.
$this->gnExtractVInfoIP();
$this->gnExtractVInfoRequestPage ();
$this->gnExtractVInfoURL();
$this->gnExtractVInfoRequestMethod();
$this->gnExtractVInfoUA();
$this->gnExtractVInfoBrowser();
$this->gnExtractVInfoOS();
$this->gnExtractVInfoReferer();
$this->gnExtractVInfoCountry();
$this->gnExtractVInfoProxyUsed();
$this->gnExtractVInfoProxyIP();
$this->gnExtractVInfoProxySignature();
$this->gnExtractVInfoAcceptedLanguage();
$this->gnExtractVInfoAcceptedEncoding();
$this->gnExtractVInfoAcceptedCharset();
$this->needExtract=0;
}
function gnServerDetails($serverName) {
//return the country code
$db = $this->db;
$ip = gethostbyname($serverName);
// $this->debug("$ip = gethostbyname($serverName);") ;
$longip=ip2long("$ip");
$sql = sprintf("SELECT c2code, c3code, country FROM iplookup WHERE %u >= ip_from AND %u <= ip_to",$longip,$longip);
$db->query($sql);
$this->debug('SQL:'.$sql.':'.$db->nf());
if($db->nf()>0) {
$db->next_record();
$serverDetail['c2code'] =$db->f("c2code");
$serverDetail['c3code'] =$db->f("c3code");
$serverDetail['country'] =$db->f("country");
} else {
$serverDetail['c2code'] ='Locate Manually';
$serverDetail['c3code'] ='Locate Manually';
$serverDetail['country'] ='Locate Manually';
}
return $serverDetail;
}
function gnLangDetails($langCode) {
//return the language Name for the given codes
$db = $this->db;
$lDetailsArr=array();
if(!empty($langCode) && is_array($langCode)) {
$lC="'".implode("','",$langCode)."'";
} else {
$lC="'$langCode'";
}
$sql = "SELECT langCode,langName FROM langCodes WHERE langCode IN ($lC)";
$this->debug('SQL : '. $sql);
$db->query($sql);
if($db->nf() > 0) {
while($db->next_record()) {
$lDetailsArr[$db->f("langCode")]=$db->f("langName");
}
} else {
return $lDetailsArr;
}
return $lDetailsArr;
}
}
?>