Thu Oct 30, 2014 9:18 am
<?php
$mysql_server = "mysql_server.com";
$mysql_user_name = "UserName";
$mysql_user_pass = "Password";
$ip = $_SERVER['REMOTE_ADDR'];
$ipno = Dot2LongIPv6($ip);
$link = mysql_connect($mysql_server, $mysql_user_name, $mysql_user_pass) or die("Could not connect to MySQL database");
// Connect to the IP2Location database
mysql_select_db("ip2location") or die("Could not select database");
// SQL query string to match the recordset that the IP number fall between the valid range
$query = "SELECT time_zone, country_code FROM db11lite WHERE $ipno >= ip_from AND $ipno <= ip_to";
// Execute SQL query
$result = mysql_query($query) or die("IP2Location Query Failed");
// Retrieve the recordset (only one)
$row = mysql_fetch_object($result);
// Keep the country information into two different variables
$country_code = $row->country_code;
$time_zone = $row->time_zone;
mysql_free_result($result); mysql_close($link);
$utc_time = gmdate("H:i:s");
$utc_h = explode(':', $utc_time);
if (strcmp($time_zone, "-") == 0) {
$localdate = $utc_time;
}
else {
$time = explode(':', $time_zone);
if ($utc_h[0] + $time[0] < 0) {
$hour = $utc_h[0] + 24 + $time[0];
}
elseif ($utc_h[0] + $time[0] >= 24) {
$hour = $utc_h[0] + $time[0] - 24;
}
else {
$hour = $utc_h[0] + $time[0];
}
$localdate = $hour . gmdate(":i:s");
}
date_default_timezone_set("Asia/Kuala_Lumpur");
$date = date_create($localdate);
echo 'Country Code: ' . $country_code . '<br>';
echo 'Local Time: ' . date_format($date, "H:i:s"). '<br>';
echo 'Server Time: ' . strftime("%H:%M:%S") . '<br>';
echo 'UTC Time: ' . gmdate("H:i:s") . '<br>';
// Function to convert IP address to IP number (IPv6)
function Dot2LongIPv6 ($IPaddr) {
$int = inet_pton($IPaddr);
$bits = 15;
$ipv6long = 0;
while($bits >= 0){
$bin = sprintf("%08b", (ord($int[$bits])));
if($ipv6long){
$ipv6long = $bin . $ipv6long;
}
else{
$ipv6long = $bin;
}
$bits--;
}
$ipv6long = gmp_strval(gmp_init($ipv6long, 2), 10);
return $ipv6long;
}
?>
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.