#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    256
    Rep Power
    12

    Showing Visitor Location on Website


    Not sure if this is the right forum but I assume the answer involves js/jquery so I took a chance.

    I am trying to have a simple "Hi, you are in city name" display when someone visits my site. I know that I do NOT want to use anything that makes them have to share their location like with HTML5 bit I found.

    I know there are a ton of services but I can't seem to get a straight answer of what I need to do to actually implement it.

    One direction I've tried to go: I see that http://freegeoip.net/json/ gets the exact data I need, but now I need that to implement on my webpage so I can display it for the users.??

    I also can't seem to figure out how to use the google api, but if that one needs the user to share the information, it wouldn't work for me anyway.

    I'm not totally sure of how it finds the data because some sites that I go to seem to be spot on, while others are off by a bit but my IP remains the same.

    Regardless...looking for some help and it would be greatly appreciated.
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,041
    Rep Power
    119
    You can use PHP json_decode to convert, that JSON file to plain text. Hope this helps you out.

    Example:
    PHP Code:
    <?php

    $json 
    file_get_contents("http://freegeoip.net/json/");

    $obj json_decode($json);
    print 
    $obj->{'ip'}; // displays IP address

    ?>
    Last edited by web_loone08; May 21st, 2013 at 10:42 PM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    256
    Rep Power
    12
    Originally Posted by web_loone08
    You can use PHP json_decode to convert, that JSON file to plain text. Hope this helps you out.

    Example:
    PHP Code:
    <?php

    $json 
    file_get_content("http://freegeoip.net/json/");

    $obj json_decode($json);
    print 
    $obj->{'Ip'}; // displays Ip address

    ?>
    Brilliant thank you! Now it seems the problem is that it's only getting the data for the server the page is hosted on, not the visitors information...hmmm
  6. #4
  7. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,041
    Rep Power
    119
    Ok, try this then:
    PHP Code:
    <?php 

    $currentIP 
    $_SERVER["REMOTE_ADDR"];
    $jsonURL "http://freegeoip.net/json/".$currentIP."";
    $json file_get_contents($jsonURL);

    $obj json_decode($json); 

    echo 
    "Hi, you are in ";
    echo 
    $obj->{"city"};
    echo 
    ", ";
    echo 
    $obj->{"region_code"};

    ?>
    Last edited by web_loone08; May 21st, 2013 at 10:39 PM. Reason: Created a more accurate code snippet; after reviewing previously posted code snippet.
  8. #5
  9. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,041
    Rep Power
    119
    And... since this is the JavaScript Forum; let me show you how to do this with PHP and JavaScript. Below I am creating a external JavaScript file using PHP. If you create the file below and embed it in a web page; it should give you; exactly what you came here looking for.

    [php=geoLocate.php]<?php
    header("Content-type:application/javascript");
    $currentIP = $_SERVER["REMOTE_ADDR"];
    $jsonURL = "http://freegeoip.net/json/".$currentIP."";
    $json = file_get_contents($jsonURL);
    ?>var json = <?php
    echo "$json";
    ?>;

    var city_and_state = "Hi, you are in " + json["city"] + ", " + json["region_code"];

    function getLocation()
    {
    var local = document.createElement("div");
    local.id="cityANDstate";
    local.innerHTML = city_and_state;
    document.body.appendChild(local);
    }

    window.onload = function(){
    new getLocation();
    }[/php]

    HTML Page (were you embed external JavaScript file)

    Code:
    <script src="geoLocate.php"></script>
    Last edited by web_loone08; May 21st, 2013 at 10:39 PM. Reason: Created a more accurate code snippet; after reviewing previously posted code snippet.

IMN logo majestic logo threadwatch logo seochat tools logo