Tracking user Visits
<?php
// setcookie("userdetail", "", time() - 3600);
// echo $_COOKIE['userdetail'];
$emailEncode = isset($_GET['emailencode'])?$_GET['emailencode']:'';
//get IP address
$ip_address='';
//whether ip is from share internet
if (!empty($_SERVER['HTTP_CLIENT_IP']))
{
$ip_address = $_SERVER['HTTP_CLIENT_IP'];
}
//whether ip is from proxy
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
//whether ip is from remote address
else
{
$ip_address = $_SERVER['REMOTE_ADDR'];
}
date_default_timezone_set('Asia/Kolkata');
//get browser details
// http://www.php.net/manual/en/function.get-browser.php#101125
function getBrowser() {
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$bname = 'Unknown';
$platform = 'Unknown';
$version= "";
// First get the platform?
if (preg_match('/linux/i', $u_agent)) {
$platform = 'linux';
} elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
$platform = 'mac';
} elseif (preg_match('/windows|win32/i', $u_agent)) {
$platform = 'windows';
}
// Next get the name of the useragent yes seperately and for good reason
if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent)) {
$bname = 'Internet Explorer';
$ub = "MSIE";
} elseif(preg_match('/Firefox/i',$u_agent)) {
$bname = 'Mozilla Firefox';
$ub = "Firefox";
} elseif(preg_match('/Chrome/i',$u_agent)) {
$bname = 'Google Chrome';
$ub = "Chrome";
} elseif(preg_match('/Safari/i',$u_agent)) {
$bname = 'Apple Safari';
$ub = "Safari";
} elseif(preg_match('/Opera/i',$u_agent)) {
$bname = 'Opera';
$ub = "Opera";
} elseif(preg_match('/Netscape/i',$u_agent)) {
$bname = 'Netscape';
$ub = "Netscape";
}
// finally get the correct version number
$known = array('Version', $ub, 'other');
$pattern = '#(?<browser>' . join('|', $known) . ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
if (!preg_match_all($pattern, $u_agent, $matches)) {
// we have no matching number just continue
}
// see how many we have
$i = count($matches['browser']);
if ($i != 1) {
//we will have two since we are not using 'other' argument yet
//see if version is before or after the name
if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){
$version= $matches['version'][0];
} else {
$version= $matches['version'][1];
}
} else {
$version= $matches['version'][0];
}
// check if we have a number
if ($version==null || $version=="") {$version="?";}
return array(
'userAgent' => $u_agent,
'name' => $bname,
'version' => $version,
'platform' => $platform,
'pattern' => $pattern
);
}
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on')
$url = "https://";
else
$url = "http://";
// Append the host(domain name, ip) to the URL.
$url.= $_SERVER['HTTP_HOST'];
// Append the requested resource location to the URL
$url.= $_SERVER['REQUEST_URI'];
$browser = getBrowser()['name'];
// getting operating system
function get_operating_system() {
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$operating_system = 'Unknown Operating System';
// return $operating_system;
$start = strpos($u_agent , "(");
$last = strpos($u_agent , ")");
$str = substr($u_agent , $start+1 , (($last-$start)-1));
$str = explode(';',$str);
// return $str[0].' ,'.$str[1];
return $str[1];
}
$os = get_operating_system();
// $pagename = basename($_SERVER['PHP_SELF']);
$pagename = $url;
// echo $pagename;
// now try it
$Actualcookie = isset($_COOKIE['userdetail'])?$_COOKIE['userdetail']:'';
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$date = date('Y-m-d H:i:sa');
$camp_date = date('Y-m-d');
$cookie_value = $_SERVER['HTTP_USER_AGENT'].':'.date('Y-m-d H:i:sa').':'.$ip_address.':'.rand().':'.microtime().':'.uniqid();
$hash_value = hash('sha512', $cookie_value);
$cookie_name = "userdetail";
//get the reverse DNS record :
$rdns = '';
$pd = $ip_address;
$ar = explode('.',$pd);
$rev = array_reverse($ar);
$rev = implode('.',$rev);
$p = $rev;
$d = $p.".in-addr.arpa";
$info = shell_exec('dig -x '.escapeshellarg($pd).'| grep '.$d);
if(count(explode("PTR",$info))>2){
$rdns = explode("PTR",$info)[2];
}else{
$rdns = '-';
}
//get geolocation
function get_ip_detail($ip){
$ip_response = file_get_contents('http://ip-api.com/json/'.$ip);
$ip_array=json_decode($ip_response);
return $ip_array;
}
$ipdarre = "157.45.207.140";
// echo $ip_address;
// $ipInfo = json_decode(grabIpInfo($ipdarre));
$ipInfo = get_ip_detail($ip_address);
$country = $ipInfo->country;
// $latitude = $ipInfo->geo->latitude;
// $longitude = $ipInfo->geo->longitude;
// echo $country." ".$latitude." ".$longitude;
$city = $ipInfo->city;
$lat_log = $city;
//check for cookie existance
if(isset($_COOKIE['userdetail'])){
$sel = mysqli_query($conn,"SELECT `id` FROM `cookie_list`WHERE `cookies`='$Actualcookie'");
$numrow = mysqli_num_rows($sel);
if($numrow){
// echo "okay ".$numrow;
$r = mysqli_fetch_assoc($sel);
$cook_id = $r['id'];
$insertIntoVisitor = mysqli_query($conn , "INSERT INTO `visitors`(`cookies_id`,`date`,`date_updated`,`ip_address`,`rDNS`,`page`,`os`,`browser`,`country`,`lat_log`) VALUES('$cook_id','$date','$date','$ip_address','$rdns','$pagename','$os','$browser','$country','$lat_log')");
}else{
$insert = mysqli_query($conn , "INSERT INTO `cookie_list`(`cookies`,`date`,`date_updated`,`uuid`,`camp_date`) VALUES ('$hash_value','$date','$date','$emailEncode','$camp_date')");
if($insert){
setcookie(
$cookie_name,
$hash_value,
time() + (10 * 365 * 24 * 60 * 60)
);
// $last_id = mysqli_insert_id($conn);
// sleep(2);
// $sel = mysqli_query($conn,"SELECT `id` FROM `cookie_list`WHERE `cookies`='$hash_value'");
// $numrow = mysqli_num_rows($sel);
// if($numrow){
// // echo "okay ".$numrow;
// $r = mysqli_fetch_assoc($sel);
// $cook_id = $r['id'];
$cook_id = mysqli_insert_id($conn);
$insertIntoVisitor = mysqli_query($conn , "INSERT INTO `visitors`(`cookies_id`,`date`,`date_updated`,`ip_address`,`rDNS`,`page`,`os`,`browser`,`country`,`lat_log`) VALUES('$cook_id','$date','$date','$ip_address','$rdns','$pagename','$os','$browser','$country','$lat_log')");
// }
}
}
}else{
$insert = mysqli_query($conn , "INSERT INTO `cookie_list`(`cookies`,`date`,`date_updated`,`uuid`,`camp_date`) VALUES ('$hash_value','$date','$date','$emailEncode','$camp_date')");
if($insert){
setcookie(
$cookie_name,
$hash_value,
time() + (10 * 365 * 24 * 60 * 60)
);
// sleep(2);
// $last_id = mysqli_insert_id($conn);
// $sel = mysqli_query($conn,"SELECT `id` FROM `cookie_list`WHERE `cookies`='$hash_value'");
// $numrow = mysqli_num_rows($sel);
// if($numrow){
// // echo "okay ".$numrow;
// $r = mysqli_fetch_assoc($sel);
// $cook_id = $r['id'];
$cook_id = mysqli_insert_id($conn);
$insertIntoVisitor = mysqli_query($conn , "INSERT INTO `visitors`(`cookies_id`,`date`,`date_updated`,`ip_address`,`rDNS`,`page`,`os`,`browser`,`country`,`lat_log`) VALUES('$cook_id','$date','$date','$ip_address','$rdns','$pagename','$os','$browser','$country','$lat_log')");
// }
}
}
?>
Comments
Post a Comment