1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to sniff the IP address of a vistitor to the web site?

Discussion in 'PHP' started by Complete, Nov 27, 2015.

  1. #1
    Hello Forum!

    I apologize if this has already been asked and answered here.

    How do I code my web site so that I can sniff out the IP address of the person visiting the site? I want to then redirect the user according to their IP address. Specifically, if the user is coming from an Arabic or Farsi country state, I want to redirect them to a different page (then if they specifically request to see the content in English, I will remember this by a cookie).
     
    Complete, Nov 27, 2015 IP
  2. billzo

    billzo Well-Known Member

    Messages:
    961
    Likes Received:
    278
    Best Answers:
    15
    Trophy Points:
    113
    #2
    $_SERVER['REMOTE_ADDR']

    http://php.net/manual/en/reserved.variables.server.php

    You will need a database or some other way of determining the location of the IP address. MaxMind provides such a database but it is not cheap. It appears that MaxMind does provide a free GeoLite2 IP address location database, but it is, according to their words, not as accurate and is only updated once per month. And there may be more.

    http://dev.maxmind.com/geoip/geoip2/geolite2/
    http://www.ipinfodb.com/ip_database.php

    This is an interesting topic. If anybody else knows of free resources to determine locations of IP addresses, please suggest.
     
    billzo, Nov 28, 2015 IP
  3. KangBroke

    KangBroke Notable Member

    Messages:
    1,026
    Likes Received:
    59
    Best Answers:
    4
    Trophy Points:
    265
    #3
    What about those who are proxy surfing?
     
    KangBroke, Nov 28, 2015 IP
  4. Helge Sverre

    Helge Sverre Prominent Member Affiliate Manager

    Messages:
    840
    Likes Received:
    99
    Best Answers:
    2
    Trophy Points:
    305
    Digital Goods:
    2
    #4
    <?php
    
    // If they are behind a proxy that sets the x-forwarded-for header
    if ($_SERVER['HTTP_X_FORWARDED_FOR']){
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    
    Code (markup):
    One can spoof the X-Forwarded-For header, so sanitize it before storing it in a database or writing it to a file.
     
    Helge Sverre, Nov 30, 2015 IP
  5. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #5
    We've had good result with this one which is free and updated fairly regularly: https://lite.ip2location.com/database-ip-country

    You query it based on a range so you don't need to store an entire IP database which is ridiculously large.

    If your primary target user would be speaking Arabic or Farsi just make that the default, and have a clear link to an english version. Usually somewhere in the top right corner or top right area of the top navigation is the most recognized area to put an option to change language, currency, etc.
     
    jestep, Dec 2, 2015 IP