HTML5 Geolocation Data

HTML5 gives you the ability to capture the geolocation information from the browser of your visitor. They will be prompted to allow the access, but after they grant it – the information shown below will become available to your JavaScript application.

Your Geolocation Data:

JavaScript:

function GeoSuccess(position) {
  var latitude = position.coords.latitude;
  var longitude = position.coords.longitude;
  var accuracy = position.coords.accuracy
  var altitude = position.coords.altitude;
  var altitudeAccuracy = position.coords.altitudeAccuracy;
  var heading = position.coords.heading;
  var speed = position.coords.speed;
 	document.getElementById('pre').innerHTML = "Latitude: "+latitude+"<br />Longitude: "+longitude+"<br />Accuracy: "+accuracy+"<br />Altitude: "+altitude+"<br />AltitudeAccuracy: "+altitudeAccuracy+"<br />Heading: "+heading+"<br />Speed: "+speed; 
}
function error(msg) {
  error('There was an error pulling your location');
}
if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(GeoSuccess, error);
} else {
  error('Geolocation not supported');
}
Flatlogic Admin Templates banner

What is my IP Address?

Your IP Address is:

3.238.204.31


PHP:

Read more on the $_SERVER variable.

$ip_address = $_SERVER['REMOTE_ADDR'];

JavaScript:

You need to call a server-side script to accomplish this. First create a PHP file on your server and paste this in it:

<?php
header('content-type: application/json; charset=utf-8');

$data = json_encode($_SERVER['REMOTE_ADDR']);
echo $_GET['callback'] . '(' . $data . ');';

Next, use this JavaScript to call the PHP file you created above.

<script type="application/javascript">
function getip(json){
    alert(json); // alerts the ip address
}
</script>
<script type="application/javascript" src="http://www.anotherdomain.com/file.php?callback=getip" ></script>

Flatlogic Admin Templates banner