Hi, I'm a beginner of PHP. I have a php code (see 1st code) for tracking visitor's IP information. It works fine when I call it from hyperlink. I want to use it to track each page view but don't want to have the same long code embedded in each page. I want the code in a class or script so that I can call it from each page using a simple code such like
//----------------- Index.html -----------
<html>
<head>
.....................
</head>
<body>
<?php
//************ call a PHP function TrackIP and carry over the file name of this page,
//************ e.g., index.html, so that the function knows which page is called from. *********
$tip= TrackIP(this page's name);
?>
</body>
</html>
Can anyone be kindly to modify the code for me? Thank you very much. (geosuite2008@gmail.com)
//*** 1st code **************
<?php
function validip($ip) {
if (!empty($ip) && ip2long($ip)!=-1) {
$reserved_ips = array (
array('0.0.0.0','2.255.255.255'),
array('10.0.0.0','10.255.255.255'),
array('127.0.0.0','127.255.255.255'),
array('169.254.0.0','169.254.255.255'),
array('172.16.0.0','172.31.255.255'),
array('192.0.2.0','192.0.2.255'),
array('192.168.0.0','192.168.255.255'),
array('255.255.255.0','255.255.255.255')
);
foreach ($reserved_ips as $r) {
$min = ip2long($r[0]);
$max = ip2long($r[1]);
if ((ip2long($ip) >= $min) && (ip2long($ip) <= $max)) return false;
}
return true;
} else {
return false;
}
}
function getip() {
if (validip($_SERVER["HTTP_CLIENT_IP"])) {
return $_SERVER["HTTP_CLIENT_IP"];
}
foreach (explode(",",$_SERVER["HTTP_X_FORWARDED_FOR"]) as $ip) {
if (validip(trim($ip))) {
return $ip;
}
}
if (validip($_SERVER["HTTP_X_FORWARDED"])) {
return $_SERVER["HTTP_X_FORWARDED"];
} elseif (validip($_SERVER["HTTP_FORWARDED_FOR"])) {
return $_SERVER["HTTP_FORWARDED_FOR"];
} elseif (validip($_SERVER["HTTP_FORWARDED"])) {
return $_SERVER["HTTP_FORWARDED"];
} elseif (validip($_SERVER["HTTP_X_FORWARDED"])) {
return $_SERVER["HTTP_X_FORWARDED"];
} else {
return $_SERVER["REMOTE_ADDR"];
}
}
include('ip2locationlite.class.php');
//Load the class
$ipLite = new ip2location_lite;
$ipLite->setKey('9058e819415152c081fbcc3e4ecb5ef3a6336db4eec1fe79745fae9c27160695');
//echo "Visitor IP is " .getip();
$ip = getip();
//Get errors and locations
//$locations = $ipLite->getCity($_SERVER['REMOTE_ADDR']);
$locations = $ipLite->getCity($ip);
$errors = $ipLite->getError();
//Getting the result
//echo "<p>\n";
//echo "<strong>First result</strong><br />\n";
if (!empty($locations) && is_array($locations)) {
foreach ($locations as $field => $val) {
// echo $field . ' : ' . $val . "<br />\n";
if($field == "countryName"){
$country = $val;
} elseif($field == "regionName"){
$region = $val;
} elseif($field == "cityName"){
$city = $val;
}
}
}
date_default_timezone_set("America/Los_Angeles");
$date=date("l dS \of F Y h:i:s A") . "<br />";
$hit_count = @file_get_contents('count.txt');
$hit_count++;
@file_put_contents('count.txt', $hit_count);
header('Location: http://www.fea4you.com/download/GeoSuit ... Setup.zip'); // redirect to the real file to be downloaded
if($_SERVER['HTTP_USER_AGENT'] != 'FeedBurner/1.0 (http://www.FeedBurner.com)' &&
$_SERVER['HTTP_USER_AGENT'] != 'Web-sniffer/1.0.36 (+http://web-sniffer.net/)' &&
$_SERVER['HTTP_USER_AGENT'] != 'Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)' &&
$_SERVER['HTTP_USER_AGENT'] != 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)' &&
$_SERVER['HTTP_USER_AGENT'] != 'Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)' &&
$_SERVER['HTTP_USER_AGENT'] != 'facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)')
{
if ($_COOKIE['visitor'] == '') {
$cok = rand(10000,100000);
setcookie('visitor', $cok ,time( ) + 360*24*60*60,'/');
$poset = $cok;
}
else{
$poset = $_COOKIE['visitor'];
}
//$ip = $_SERVER['REMOTE_ADDR'];
$hostaddress = gethostbyaddr($ip);
$browser = $_SERVER['HTTP_USER_AGENT'];
$referred = $_SERVER['HTTP_REFERER']; // a quirky spelling mistake that stuck in php
$rqstURL = split("/", $_SERVER['REQUEST_URI']);
$rqstpg=$rqstURL[count($rqstURL)-1];
$rqstpg1=split("_", $rqstpg);
$rqstpg=$rqstpg1[0];
$all = " ".date('m/d/Y')." ".date('H:i:s').
" \ ".$country.
" \ ".$region.
" \ ".$city.
" \ ".$rqstpg.
" \ ".$ip. "\n";
$adr="visitors.txt";
$file_han=fopen($adr,"a");
fwrite($file_han,$all);
fclose($file_han);
}
?>
//*********** class called *********************
<?php
final class ip2location_lite{
protected $errors = array();
protected $service = 'api.ipinfodb.com';
protected $version = 'v3';
protected $apiKey = '';
public function __construct(){}
public function __destruct(){}
public function setKey($key){
if(!empty($key)) $this->apiKey = $key;
}
public function getError(){
return implode("\n", $this->errors);
}
public function getCountry($host){
return $this->getResult($host, 'ip-country');
}
public function getRegion($host){
return $this->getResult($host, 'ip-region');
}
public function getCity($host){
return $this->getResult($host, 'ip-city');
}
private function getResult($host, $name){
$ip = @gethostbyname($host);
if(preg_match('/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/', $ip)){
$xml = @file_get_contents('http://' . $this->service . '/' . $this->version . '/' . $name . '/?key=' . $this->apiKey . '&ip=' . $ip . '&format=xml');
try{
$response = @new SimpleXMLElement($xml);
foreach($response as $field=>$value){
$result[(string)$field] = (string)$value;
}
return $result;
}
catch(Exception $e){
$this->errors[] = $e->getMessage();
return;
}
}
$this->errors[] = '"' . $host . '" is not a valid IP address or hostname.';
return;
}
}
?>

