How to check what country is my website's vistor from?

Discussion in 'Programming' started by MarcinST3333, Feb 1, 2009.

  1. #1
    I'm building a new website and need to know what country is the visitor from so I can dynamically display the right content for them (ie France - french content etc).

    I know it's possible, many sites do it.

    I'd like to do it using PHP if possible. Any ideas? Thanks!
     
    MarcinST3333, Feb 1, 2009 IP
  2. NuLLByTe

    NuLLByTe Active Member

    Messages:
    382
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    80
    #2
    <?php
    $ip = "xx.xxx.xxx.xx";
    $file = file_get_contents("http://www.melissadata.com/lookups/iplocation.asp?ipaddress=" . $ip);
    preg_match('^<tr><td align="right">Country</td><td><b>([A-Z]+)</b></td></tr>^',$file , $country);
    $country = $country[1];
    if(empty($country)) {
    echo "Invalid IP";
    }else{
    echo ucfirst(strtolower($country));
    }
    ?>
    PHP:
    Hope this helps you.
     
    NuLLByTe, Feb 1, 2009 IP
    harrisunderwork and anujbux like this.
  3. harrisunderwork

    harrisunderwork Well-Known Member

    Messages:
    1,005
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    135
    #3
    +1 to above
     
    harrisunderwork, Feb 1, 2009 IP
  4. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #4
    in addition to what NuLLByTe said
    you can change $ip = "xx.xxx.xxx.xx"; to $ip = $_SERVER['REMOTE_ADDR'];
     
    crivion, Feb 1, 2009 IP
  5. MarcinST3333

    MarcinST3333 Peon

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks a mill guys!
     
    MarcinST3333, Feb 2, 2009 IP
  6. anujbux

    anujbux Well-Known Member

    Messages:
    237
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #6
    reeped nullbyte
     
    anujbux, Feb 2, 2009 IP
  7. buldozerceto

    buldozerceto Active Member

    Messages:
    1,137
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    88
    #7
    buldozerceto, Feb 2, 2009 IP
  8. MarcinST3333

    MarcinST3333 Peon

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I got this error when trying to import the database:
    Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 8216437 bytes) in URL/import.php on line 101

    any ideas?
     
    MarcinST3333, Feb 2, 2009 IP
  9. ignitesystems

    ignitesystems Peon

    Messages:
    26
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    To resolve the php error you get:


    For those of you who use shared hosting the quick solution would be to add a line in .htaccess file located at your root folder, or you may need to create a file called php.ini in your hosting account root.

    This is the command:

    php_value memory_limit 24M

    here 24M is an example and most of the times this is enough to get your site back, contact your host support if this doesnt work.

    First step for this is to locate your php.ini file in the server and to see what is the memory limit (this is shown in the error message). Create a file with the below code and name it something like “test.php”
     
    ignitesystems, Feb 4, 2009 IP