Finding Client’s IP address, Country, State, City using NetIP.de api in PHP

PHP

If you want to get client’s IP address you will use $_SERVER[‘REMOTE_ADDR’] function in PHP.
But you will not get the real IP address of the client. To get the real IP address You can use the method I used.
Also With this method you can also find the City, State, Country client’s in.
The code is pretty simple. With the help of NetIP.de it was pretty easy.

[email protected]_get_contents(‘http://www.netip.de’);if (empty($response)){throw new InvalidArgumentException(“Error contacting Geo-IP-Server”);}$patterns=array();$patterns[“state”] = ‘#State/Region: (.*?);$patterns[“IP”] = ‘#IP: (.*?) #i’;$patterns[“country”] = ‘#Country: (.*?) #i’;$patterns[“city”] = ‘#City: (.*?) $pattern){$ipInfo[$key] = preg_match($pattern,$response,$value) && !empty($value[1]) ? $value[1] : ‘not found’;} $uct=$ipInfo[“state”];$uip=$ipInfo[“IP”];$uci=$ipInfo[“city”];$ipInfo=explode(‘-‘,$ipInfo[“country”]);$ucs=$ipInfo[0];$uco=$ipInfo[1];

Below are the variables for different purposes.

$uip //IP address
$uco //Client’s Country
$uci //Client’s City
$ucs //Client’s Country in Short Form(eg: if user is from INDIA then the variable will be “IN”)
$uct //Client’s State

You should echo the variables as needed. Hope it helps.