Can you use PHP code to display location specific data?

Discussion in 'PHP' started by martyn11post, Feb 19, 2008.

  1. #1
    Basically I want to display a simple piece of code (jscript) for uk visitors, and a different script for others.


    I'm guessing I could possibly detect locations using php then output the correct code accordingly?

    Any help much appreciated,
    Martyn :)
     
    martyn11post, Feb 19, 2008 IP
  2. 00johnny

    00johnny Peon

    Messages:
    149
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Yes, you would want to use something to covert the users ip address to a geographic location. Google maps' Geocoder may do this, http://www.trueknowledge.com/api/ is another.
    Then just have a few checks to see if they are in the country that needs the proper script.
     
    00johnny, Feb 19, 2008 IP
  3. stoli

    stoli Peon

    Messages:
    69
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #3
    What you are talking about is geolocating an IP address.

    There are scripts to do that here:
    http://www.tellinya.com/read/2007/06/03/2.html
    and here:
    http://www.ardamis.com/2008/01/27/how-to-geolocate-visitors-using-an-ip-to-country-database/

    These create databases on your server with lookup tables of IP ranges.

    Alternatively you could use hostip.info. Here is an example script to do that:
    <?php
    
    $ip=$_SERVER['REMOTE_ADDR']; 
    
    $country = file_get_contents("http://api.hostip.info/country.php?ip=".$ip);
    
    echo "Your IP address is $ip<br />\n";
    echo "Your country code is $country<br />\n";
    
    if ($country == "XX") {
      echo "Unable to locate your country";
    }
    elseif ($country == "UK") {
      echo "You are in the UK";
    }
    else {
      echo "You are NOT in the UK.";
    }
    
    ?>
    PHP:
     
    stoli, Feb 19, 2008 IP
  4. martyn11post

    martyn11post Guest

    Messages:
    42
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Cheers stoli,

    That gives me something to work with...

    Makes me wonder whether I could use it to change language of site to suit the visitor, although that may be going to far!

    Thanks for the code and links,
    Martyn
     
    martyn11post, Feb 19, 2008 IP
  5. stoli

    stoli Peon

    Messages:
    69
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Glad to be able to help.

    Once you have geolocated a user and stored that location as part of a session, then it is pretty much unlimited how far you want to go with specific regional variations, so long as it degrades when you are unable to locate them and you provide the option to change location.
     
    stoli, Feb 19, 2008 IP
  6. CATTechnologies

    CATTechnologies Guest

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    We cannot Directly get Location Name or Details but we get Ip of that location
    By ipwe can get address details.





    For more information: cattechnologies.com
     
    CATTechnologies, Feb 21, 2008 IP
  7. bpasc95

    bpasc95 Active Member

    Messages:
    196
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    70
    #7
    One we use targets down to the City:
    http://www.maxmind.com/app/geolitecity

    They also provide a PHP api to simplify integration.

    Note that they offer two versions, free and fee. The free DB is not as accurate as the fee based counterpart.

    -Bing
     
    bpasc95, Feb 21, 2008 IP
  8. mvl

    mvl Peon

    Messages:
    147
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #8
    maxmind works great, and they provide free PHP code with examples for their API. If you only need to find out the country the free geolite City db is good enough. If you want more accuracy you might want to look at the paid versions.

    Take a look at http://getmyip.eu/ for a little demonstration of what you can do with even more detailed (paid) webservices.
     
    mvl, Feb 21, 2008 IP