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).
$_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.
<?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.
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.