How to retrieve city and country through IP address in PHP - php

I am trying to make a log of users heading to my website. I want to save their IP addresses, as well as city and country info.
Thanks in Advance.

Try using MaxMind GeoIP database which I find the best solution that's freely available. It has a set of methods to retrieve the city and the country by IP and to get the ip just use:
$_SERVER['REMOTE_ADDR']

You can get the IP from $_SERVER['REMOTE_ADDR']. To get location you will have to find some service that provides lookup and check the IP you just collected.
You might also consider signing up for Google analytics.

I Think you should go for Google analytic, All these data you will get from there. Otherwise for ip address you can use this script
function getIP() {
$ip;
if (getenv("HTTP_CLIENT_IP"))
$ip = getenv("HTTP_CLIENT_IP");
else if(getenv("HTTP_X_FORWARDED_FOR"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
else if(getenv("REMOTE_ADDR"))
$ip = getenv("REMOTE_ADDR");
else
$ip = "UNKNOWN";
return $ip;
}
& for country name
http://geoip.wtanaka.com/cc/$ipaddr
this will return country ISO code.

Do the users input their City and Country? If yes then easily save the data to database.
Or do you want to track these by IP? If this one - download/buy the database of the countries by ip from http://www.ip2country.net/download.html and only put the IP into the database.

Related

Ipify returning website ip instead of user ip

So far I’m looking into using ipify’s php api for returning an ip but every time I try it returns my website ip. I want it to show user ip instead what can I do to fix that?
Here’s the code:
<?php
$ip = file_get_contents('https://api4.ipify.org');
echo "My public IP address is: " . $ip;
?>
Edit:
I understand there is this way of doing it
<?php
$ip = $_SERVER['REMOTE_ADDR'];
echo "My public IP address is: " . $ip;
?>
but i prefer $_SERVER["HTTP_CF_CONNECTING_IP"]
and doing so they dont always return 1pv4. Is there any way to make it enforce ipv4 format without modifying website settings?
This should do it, no need for the API to access the IP address of the user:
<?php
$ip = $_SERVER['REMOTE_ADDR'];
echo "My public IP address is: " . $ip;
?>
Something to note though, is that this method will get whatever the IP is, so if they are using a VPN or Proxy, you will get the IP of that service.
Nevermind i originally didnt wanna modify web settings but i secure my website with cloudflare and they give option 'Pseudo IPV4' and i set it to 'overwrite headers' and so far i havent got any ipv6 only ipv4

php currency setting based on user location

I need to change currency based on user location. This is my code
<?php
$ipaddress = $_SERVER['REMOTE_ADDR'];
$location = unserialize( file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $_SERVER['REMOTE_ADDR']) );
if($location["geoplugin_countryCode"] === "US")
{
// block to set us currency
}
else{
// user can choose their own currency from array (excluding us)
}
?>
I uploaded it on a server, and to check if the functionality works correctly or not and I used different vpn chrome extension. The problem is all the time else part is alone gets executed even when I choose us as vpn server. I don't know What is causing this problem.
Most probably the issue is that you use $_SERVER['REMOTE_ADDR'].
If you are using a proxy, you should use $_SERVER['HTTP_X_FORWARDED_FOR']

issues with $_SERVER['REMOTE_ADDR'];

currently I am trying to store my clients IP address on view, this proving to be more complicated then expected.
Here is the code I am currently using.
<?php
$ip = $_SERVER['REMOTE_ADDR'];
print $_SERVER['REMOTE_ADDR'];
$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}/json"));
$location = $details->country;
print '<br>';
print $details->country;
print '<br>';
print $details->city;
?>
which returns something that looks like this.
173.245.56.104
US
San Francisco
This IP and this location links back to a cloudflare server and not my actual IP address.
You can see that information here cf-173-245-56-104.cloudflare.com.
How ever when my buddies visit the same url which contains the document that is suppose to be giving the IP it gives them a different IP also connected to CloudFlare.
Example 1 being the following.
199.27.128.244
SG
Singapore
Which links to this cloudflare hostname cf-199-27-128-244.cloudflare.com.
And with a simple lookup you can see that it does not even provided the right GEO so for the reason I am looking to use this code, it would not even be viable if it's saying US based users are from Singapore or other countries.
So the question is, HOW can I get the REAL client IP address on visit?

Redirect user to page depending on IP address not working

The IP address of the computer that I am using is the one specified in $ip. The problem is that when $userip != $ip, the message is displayed, which it should not. The first version of the if statement is ideally how I want the page to work. Any ideas/suggestions? Thanks in advance.
Note: The purpose of the if statement is to ensure that the users access the site from a particular computer. For example, employees can only access the site from their work station computer (not from PCs). If they are not at their work station computer, then the message is displayed.
Note: I got the $ip from running an "ipconfig /a"ll command.
Note: I am currently testing this with one IP address and I will eventually test with multiple ones.
$userip = ($_SERVER['REMOTE_ADDR']);
$ip = '172.34.56.202';
if ($userip != $ip) {
echo "<h3>" . "ACCESS DENIED. Your IP address is not in the list of allowed IPs" . "</h3>";
} else{
$URL="http://www.yahoo.com";
header ("Location: $URL");
}
?>
I also tried to change around the operations of the if statement in this version, but it still doesn't work..the error message is still displayed instead of directing the user to a different page:
<?php
error_reporting(E_ALL);
$userip = ($_SERVER['REMOTE_ADDR']);
$ip = '172.34.56.202';
if ($userip == $ip) {
$URL="http://wt-ws.delta.lasalle.edu/tempProject/timeSheetProject/TimeSheet.php";
header ("Location: $URL");
} else{
echo "<h3>" . "ACCESS DENIED. Your IP address is not in the list of allowed IPs" . "</h3>";
}
?>
I just tested the above code and it works just fine on my end for the page I created.
The only thing I would suggest is doing
echo($userip);
To see what the value is for $userip. As if you are doing this on a test box installed on your machine your IP very well could be 127.0.0.1 , as you are viewing the file local.
To your updated question
When you use ipconfig /a this gives you the IP address assigned to your machine. Most networks use a subnet mask and a private IP Address. For example if you use ipconfig /a and get an IP address in the following ranges it is a private non routable IP address.
10.0.0.0 - 10.255.255.255 - Class A
172.16.0.0 - 172.31.255.255 - Class B
192.168.0.0 - 192.168.255.255 - Class C
To find your public IP Address you can do this a couple of ways:
You can google what is my ip?
www.ipchicken.com
www.whatismyip.com
Maybe try using the identical comparison operators such as !== or === ?

How can I get user's country flag on my site

I wanna display user's/visitor's country flag on my site.
I am using different technologies like php,jsp and simple html. So I want a code which by placing on my site, visitors can see and it should run in all platform.
In short I want country detection API. If anybody can help me, I'll be very thankful.
Source :
http://www.shorter.in/#flag
<img src="http://shorter.in/flag.php">
Example for the code given above.
a busy cat http://shorter.in/flag.php
I guess this is what you are looking for.
My service, ipdata.co provides an IP Geolocation API on https://api.ipdata.co and serves flags on for example https://ipdata.co/flags/cu.png.
All you have to do is know your visitors' country's iso code and you can fill it in
ipdata.co/flags/country-code.png
You can of course get the user's country code by calling https://api.ipdata.co/user-ip.
Sample embed;
<img src="https://ipdata.co/flags/us.png" alt="US Flag">
Gives
Edit
We now also provide you with the country emoji flag and country emoji unicode.
Yeah there is something already available and you don't have to reinvent the wheel.
Check this thing out.
http://api.hostip.info/flag.php?ip=12.215.42.19
Grab your user's IP using PHP and pass it to the API.
<?php
$ip=$_SERVER['REMOTE_ADDR'];
?>
Putting it all together
<?php
$ip=$_SERVER['REMOTE_ADDR'];
echo "<img src='http://api.hostip.info/flag.php?ip=$ip' />";
?>
You can use the GeoIP extension and then map the country in question to a given icon.
$countryName = geoip_country_name_by_name($_SERVER['REMOTE_ADDR']);
echo $countryName;
Note that getting the country via IP is not exact.
Get the IP of visitor.
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
Use ip2location to find the country of the user.
http://dev.maxmind.com/geoip/legacy/geolite/
Compare the resulting country to a list of images and display the matching image.
I suggest using a database to store the country name and the path to the associated image.

Categories