SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: IP Adress.....

  1. #1
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation IP Adress.....

    Hello everyone ....


    what I want to do not something new ....as I said before I'm a new php programmer so my question goes as following ..


    I want to get the user ip then compare it to the country ip he is belonging to.
    why ? because I have a select menu and contains countries name so the select menu will show the user his country when he logs the index page...
    so how can I get countries ip ? and would you please give me an advice for this task....

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    get an ip to country database or use a webservice.

    this company offers a free database along with php code samples
    http://www.maxmind.com/app/geoip_country

    the ip address of the user is stored in this variable
    $_SERVER['REMOTE_ADDR']

  3. #3
    SitePoint Enthusiast seves's Avatar
    Join Date
    Oct 2006
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    or $REMOTE_ADDR without $_SERVER
    skype: netsev
    icq:242544335

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by seves
    or $REMOTE_ADDR without $_SERVER
    that would require register_globals is turned on. php has had this setting turned off by default for years now...and will be completely eliminated in php 6

  5. #5
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's some quick code that will use airn to get the information I think you are looking for

    $IPAddr = $_SERVER['REMOTE_ADDR']
    $fh = fopen("http://ws.arin.net/cgi-bin/whois.pl?queryinput=$IPAddr", "r");
    $data = "";
    while(!feof($fh))
    {
    $output = fgets($fh, 1024);
    $f = stripos($output,"OrgName:");
    if ($f !== false)
    $ISP = substr($output,9);

    $f = stripos($output,"City:");
    if ($f !== false)
    $CITY = substr($output,6);

    $f = stripos($output,"StateProv:");
    if ($f !== false)
    $STATEPROV = substr($output,11);

    $f = stripos($output,"Country");
    if ($f !== false)
    $COUNTRY = substr($output,9);

    $f = stripos($output,"CIDR:");
    if ($f !== false)
    $IPRange = substr($output,10);
    // $data = $data . $output;
    }

    fclose($fh);

  6. #6
    Twitter: @TimIgoe silver trophy TimIgoe's Avatar
    Join Date
    Feb 2005
    Location
    Blackpool, UK
    Posts
    1,056
    Mentioned
    27 Post(s)
    Tagged
    1 Thread(s)
    To get the country from the IP, you need an IP database - the one I use is 'Geoip' (http://www.maxmind.com/app/ip-location)

    To use it, you download the PHP API from http://www.maxmind.com/download/geoip/api/php/ and the country database from http://www.maxmind.com/app/geoip_country

    then its simply a case of 4 lines of code
    PHP Code:
    include ('geoip.inc');
    $gi geoip_open("/path/to/downloaded/GeoIP.dat",GEOIP_STANDARD);
    $CountryCode geoip_country_code_by_addr($gi$_SERVER['REMOTE_ADDR']);
    geoip_close($gi); 

  7. #7
    SitePoint Zealot
    Join Date
    May 2002
    Location
    Montreal
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have been trying to do a similar thing and have a post here :
    http://www.sitepoint.com/forums/showthread.php?t=432878

    I am trying to send users from the US and Canada to one page and everyone else to another.
    Mark Dixon
    Freelance Web Designer and Developer
    www.markdixon.ca
    Montreal, Quebec, Canada



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •