How do I know which country my visitor is from?

Discussion in 'PHP' started by hexadesigns, Sep 7, 2008.

  1. #1
    Hi,

    I want to display the selling price of my product, according to
    the user's country. For example,

    USA/Canada - USD
    Europe - Euro
    UK - GBP
    Australia - AUD

    and so forth, any ideas how this can be achieved with php? :)
     
    hexadesigns, Sep 7, 2008 IP
  2. dfourss

    dfourss Well-Known Member

    Messages:
    1,271
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    128
    #2
    the visitor's ip address should tell you where they are from
     
    dfourss, Sep 7, 2008 IP
  3. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #3
    use
    $countries = array(
    'uk' => 'GBP',
    'us' => 'USD',
    'com' => USD,
    'au' => 'AUD');
    
    $hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
    preg_match('/\.([^\.]+)$/',$hostname,$out);
    $tld = $out[1];
    
    echo $countries[$tld];
    PHP:
    That should work for the majority, although it's not an exact science. Add the tld to the countries list along with the currency type (you can extend the array to hold different info too, this was just an example)
     
    JAY6390, Sep 7, 2008 IP
  4. ammoun

    ammoun Active Member

    Messages:
    983
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    60
    #4
    I think he might need a IP adresses database? :D
     
    ammoun, Sep 7, 2008 IP
  5. hexadesigns

    hexadesigns Peon

    Messages:
    350
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks Jay6390, but i believe I am looking for what ammoun said. I need exact location based on the visitor's IP. Is this something that is quite complex or some readymade solutions are available?
     
    hexadesigns, Sep 7, 2008 IP
  6. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #6
    JAY6390, Sep 7, 2008 IP
    hexadesigns likes this.
  7. ilook

    ilook Well-Known Member

    Messages:
    1,602
    Likes Received:
    15
    Best Answers:
    1
    Trophy Points:
    165
    #7
    You need a ip2country database
     
    ilook, Sep 7, 2008 IP
  8. Steve136

    Steve136 Peon

    Messages:
    240
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hi,

    There is a PHP module that can be installed that'll do just that, it will tell you exactly what country an IP address is located in.

    http://uk3.php.net/manual/en/ref.geoip.php

    Regards,

    Steve
     
    Steve136, Sep 7, 2008 IP
    hexadesigns likes this.
  9. hexadesigns

    hexadesigns Peon

    Messages:
    350
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks Jay and Steve, I think I have got the solution. Enjoy some green. Both of you :D
     
    hexadesigns, Sep 7, 2008 IP
  10. littlejohn199

    littlejohn199 Peon

    Messages:
    42
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #10
    littlejohn199, Apr 25, 2011 IP