How to find

detect the visitor's IP address

The visitor's IP (or the proxy) address may be found in a few environment variables.
Even for this site, $_SERVER["REMOTE_ADDR"] is the good one in most cases.
The other variables are usually empty or difficult to analyse. This is generally the case for HTTP_VIA, HTTP_X_COMING_FROM, HTTP_X_FORWARDED, HTTP_COMING_FROM, HTTP_FORWARDED_FOR and HTTP_FORWARDED.
HTTP_X_FORWARDED_FOR sometimes allows to get a more precise information. This variable may be set as unknown, one or a few IP addresses separated by commas or spaces.
Thus we have to be sure that we can use its value.
We have to ignore the following special IP address blocks (non-public) :

  • 10.0.0.0 - 10.255.255.255
  • 127.0.0.0 - 127.255.255.255
  • 169.254.0.0 - 169.254.255.255
  • 172.16.0.0 - 172.31.255.255
  • 192.168.0.0 - 192.168.255.255
  • 224.0.0.0 - 239.255.255.255

Then any text.


$forwarded = $_SERVER["HTTP_X_FORWARDED_FOR"];
if($forwarded)
{
$forwarded = preg_split("/,| /",$forwarded);
$forwarded = $forwarded[0];
if (preg_match("/^((10|127|22[4-9]|23[0-9])\.\d{1,3}|172\.(1[6-9]|2\d|3[0-1])|192\.168|169\.254)(\.\d{1,3}){2}$/",$forwarded)){$forwarded = "";}
if (!preg_match("/^([0-9]{1,3}(\.|$)){4}/",$forwarded) && $forwarded){$forwarded = "";}
if($forwarded){$no_ip = $forwarded;}
}
if(!$forwarded){$no_ip = $_SERVER["REMOTE_ADDR"];}
/*Result :
$no_ip="3.235.76.155";*/

Your IP address: 3.235.76.155

detect the visitor's country

All the following methods are more or less reliable.
As for you:
1. unknown 2. UNITED STATES 3. unknown 4. unknown 5. unknown 6. UNITED STATES 7. UNITED STATES
Most probable: UNITED STATES

1. Results from freegeoip

Service unavailable / php function file () disabled by the provider since 2008

Country:

City:

Quite reliable with: most big countries.

Unreliable with: AOL, tele2, chello, colt, large companies.

Code with your IP address (3.235.76.155) :

$no_ip=$_SERVER["REMOTE_ADDR"]; /*$no_ip="3.235.76.155";*/
/*or $no_ip=getenv("REMOTE_ADDR");*/
$hostip = "https://freegeoip.app/json/3.235.76.155";
$result = file_get_contents($hostip);
$result = json_decode($result);
/*
In the object $result, we will find:
$result->country_code : (thus )
$result->city :
$result->latitude :
$result->longitude :
*/

Results from extreme-ip

Service unavailable / php function file () disabled by the provider since 2008

Country:

City:

Latitude :

Longitude:

Quite reliable with: most big countries.

Unreliable with: AOL, tele2, chello, colt, large companies.

Code with your IP address (3.235.76.155) :

$no_ip=$_SERVER["REMOTE_ADDR"]; /*$no_ip="3.235.76.155";*/
/*or $no_ip=getenv("REMOTE_ADDR");*/
$hostip = "https://extreme-ip-lookup.com/json/3.235.76.155";
$result = file_get_contents($hostip);
$result = json_decode($result);
/*
In the object $result, we will find:
$result->country :
$result->city :
$result->lat :
$result->lon :
*/

2. ip-to-country

Country: UNITED STATES

Quite reliable with: most big countries.

Unreliable with: AOL, tele2, chello, colt, large companies, African Internet Numbers Registry, Asian-Pacific Numbers.

Code with your IP address (3.235.76.155) :

`origin` is the table created from the ip-to-country download

$paysdorigine="unknown";
$no_ip=$_SERVER["REMOTE_ADDR"]; /*$no_ip="3.235.76.155";*/
/*or $no_ip=getenv("REMOTE_ADDR");*/
$serveur=mysql_connect(host,username,password,database);/*Enter your MySQL personal info here*/
$res=mysql_query($serveur,"SELECT COUNTRY_NAME FROM `origin`
WHERE inet_aton('$no_ip')>=IP_FROM AND inet_aton('$no_ip')<=IP_TO;");
$ref=mysql_fetch_row($res);
if($ref[0]){$paysdorigine=$ref[0];}
/*Result :
$paysdorigine="UNITED STATES"; */

More information
As IP-to-country database is not updated, using GeoIP legacy databases may be a better option.

3. Domain

Host: ec2-3-235-76-155.compute-1.amazonaws.com

Country: unknown

Quite reliable with: most countries, tele2, chello.

No clue with: extensions .com, .net,.org ...

Code with your Host (ec2-3-235-76-155.compute-1.amazonaws.com / 3.235.76.155) :

`origin` is the table created from the ip-to-country download

$serveur=mysql_connect(host,username,password,database);/*Enter your MySQL personal info here*/
$paysdorigine="unknown";
$no_ip=$_SERVER["REMOTE_ADDR"]; /*$no_ip="3.235.76.155";*/
/*or $no_ip=getenv("REMOTE_ADDR");*/
$hote=gethostbyaddr($no_ip); /*$hote="ec2-3-235-76-155.compute-1.amazonaws.com";*/
if ($hote!="")
 {
 $inconnuhote=explode('.',$hote);
 $inconnuextension=$inconnuhote[count($inconnuhote)-1];
 $res=mysql_query($serveur,"SELECT DISTINCT COUNTRY_NAME FROM `origin` WHERE COUNTRY_CODE='$inconnuextension'");
 $ref=mysql_fetch_row($inconnures);
 if ($ref[0]){$paysdorigine=$ref[0];}
 }
/*Result :
$paysdorigine="unknown"; */

4. Preferred language

Preferred language:

Country: unknown

Quite reliable with: AOL, MSIE.

Unreliable with: the latest versions of Opera, Mozilla, Konqueror, text-based browsers.

Code with your Preferred language () :

`origin` is the table created from the ip-to-country download

$serveur=mysql_connect(host,username,password,database);/*Enter your MySQL personal info here*/
$paysdorigine="unknown";
$accepted_language=getenv("HTTP_ACCEPT_LANGUAGE");
$accepted_language=strtoupper($accepted_language);
$preg="/^([A-Z]{2}-)?([A-Z]{2})/";
preg_match($preg,$accepted_language,$lang);
$acceptlang=preg_replace($preg,"$2",$lang[0]);
 switch ($acceptlang)
  {
  case "EL" : $paysdorigine="GREECE";break;
  case "HE" : $paysdorigine="ISRAEL";break;
  case "SE" : $paysdorigine="SWEDEN";break;
  case "JA" : $paysdorigine="JAPAN";break;
  default : $result=mysql_query($serveur,"SELECT DISTINCT `COUNTRY_NAME` FROM `origin` WHERE `COUNTRY_CODE`='$acceptlang'");
           $test=mysql_fetch_row($result);
           if ($test[0]){$paysdorigine=$test[0];}
  }
/*Result :
$paysdorigine="unknown"; */

5. UA language

User Agent : claudebot

Country: unknown

No clue with: MSIE.

Code with your UA language (claudebot) :

`origin` is the table created from the ip-to-country download

function getlangfromUA($string)
{
 $string=strtoupper($string);
 $preg="/(?:; |\[)(?:[A-Z]{2}-)?([A-Z]{2})(\]|; RV|\))/";
 preg_match($preg,$string,$lang);
 return $lang[1];
}
//
$serveur=mysql_connect(host,username,password,database);/*Enter your MySQL personal info here*/
$paysdorigine="unknown";
$UA=getenv("HTTP_USER_AGENT");
$accepted_language=getlangfromUA($UA);
if ($accepted_language)
{
 $accepted_language=strtoupper($accepted_language);
 $preg="/^(?:[A-Z]{2}-)?([A-Z]{2})/";
 preg_match($preg,$accepted_language,$lang);
 $acceptlang=$lang[1];
  switch ($acceptlang)
  {
  case "EL" : $paysdorigine="GREECE";break;
  case "HE" : $paysdorigine="ISRAEL";break;
  case "SE" : $paysdorigine="SWEDEN";break;
  case "JA" : $paysdorigine="JAPAN";break;
  default : $result=mysql_query($serveur,"SELECT DISTINCT `COUNTRY_NAME` FROM `origin` WHERE `COUNTRY_CODE`='$acceptlang'");
            $test=mysql_fetch_row($result);
            if ($test[0]){$paysdorigine=$test[0];}
  }
}
/*Result :
$paysdorigine="unknown"; */

Examining the referrer when present or extracting the search engine domain may also help.

6. GeoIP legacy databases

Country: UNITED STATES

Quite reliable with: most big countries.

Unreliable with: AOL, tele2, chello, colt, large companies.

Code with your IP address (3.235.76.155) :

`geolite` is the table created from the geolite download

$paysdorigine="unknown";
$no_ip=$_SERVER["REMOTE_ADDR"]; /*$no_ip="3.235.76.155";*/
/*or $no_ip=getenv("REMOTE_ADDR");*/
$serveur=mysql_connect(host,username,password,database);/*Enter your MySQL personal info here*/
$res=mysql_query($serveur,"SELECT cn FROM `geolite`
WHERE inet_aton('$no_ip')>=start AND inet_aton('$no_ip')<=end;");
$ref=mysql_fetch_row($res);
if($ref[0]){$paysdorigine=$ref[0];}
/*Result :
$paysdorigine="UNITED STATES"; */

7. DB-IP

Country: UNITED STATES

Quite reliable with: most big countries.

Unreliable with: AOL, tele2, chello, colt, large companies.

phpwhois 4.2.2

If the php functions fsockopen() and sleep() are enabled (which is not the case with free.fr), the most reliable method for a few requests is the one provided by phpwhois.
A query to the relevant whois server and all the information is returned in an Array.
The code of the country will be in the array $result['rawdata'].

`origin` is the table created from the ip-to-country download

$query = $_SERVER["REMOTE_ADDR"]; /*$query="3.235.76.155";*/
/*or $query=getenv("REMOTE_ADDR");*/
include_once('phpwhois-4.0.1/whois.main.php');
$whois = new Whois();
$result = $whois->Lookup($query);
$country=$result['rawdata'][index of the string containing country:];
$country_code= trim(str_replace("country:", "", $country));
//
$serveur=mysql_connect(host,username,password,database);/*Enter your MySQL personal info here*/
$test=mysql_query($serveur,"SELECT DISTINCT `COUNTRY_NAME` FROM `origin` WHERE `COUNTRY_CODE`='$country_code'");
$country_name=mysql_fetch_row($test);
$paysdorigine=$country_name[0];

 
cssvalidationw3cvalidation