<html>
<?php
/*
Function that uses http://www.hostip.info's API to retrieve the user's
IP address, Country, State, City, Latitude and Longitude.
*/
function get_content($url)
{
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HEADER, 0);
ob_start();
curl_exec ($ch);
curl_close ($ch);
$string = ob_get_contents();
ob_end_clean();
return $string;
}
$ipaddr = $_SERVER['REMOTE_ADDR']; //Retrieve the user's IP
$content = get_content('http://api.hostip.info/get_html.php?ip='.$ipaddr.'&position=true'); //Call the function
$reglat = "/Latitude\s*.*/"; //Regular Expression to look for the line that says Latitude and everything after it
$reglong = "/Longitude\s*.*/"; //Regular Expression to look for the line that says Longitude and everything after it
preg_match($reglat, $content, $match_lat); //Look for the regular expression in the output and save to array
preg_match($reglong, $content, $match_long); //Look for the regular expression in the output and save to array
//$pinginfo = exec("/usr/sbin/ping -v -c 1 8.8.8.8");
//echo $pinginfo;
$userlat = str_replace("Latitude: ", "", $match_lat[0]); //Remove "Latitude: " from output
$userlong = str_replace("Longitude: ", "", $match_long[0]); //Remove "Longitude: " from output
?>
<head>
<title>Individual Project</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<meta charset="UTF-8">
<style type="text/css">
#container {
width: 100%;
height: auto !important;
min-height: 40em;
}
#map_canvas {
margin: 0;
width: 80%;
height: 30em;
margin-top: 0;
margin-bottom: 0;
margin-left: auto;
margin-right: auto;
}
</style>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var map;
function initialize() {
var myOptions = {
zoom: 11,
center: new google.maps.LatLng(<?php echo $userlat; ?>, <?php echo $userlong; ?>),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map_canvas'),
myOptions);
var location = new google.maps.LatLng(<?php echo $userlat; ?>, <?php echo $userlong; ?>);
var marker = new google.maps.Marker(
{position:location,
map:map,
title:"You are here!"}
);
var infowindow = new google.maps.InfoWindow(
{
content: 'Your location and information: </br></br><?php echo $match_lat[0];?></br><?php echo $match_long[0]; ?></br><?php echo "IP Address: ".$ipaddr; ?>'
}
);
infowindow.open(map, marker);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="container">
<div id="map_canvas"></div>
</div>
</body>
</html>