How to Get Exact Location using Ip Address and ip-api In PHP
Ip Address is a fingerprint of each Computer on Internet. It enables your computer to connect with other computers. we can also get your details of location from your Ip Address. By Identifying the Geo Location of visitor we can show specific part of website to the visitor, Enable/Disable payment methods and more. How you can Identify Location of visitor using His/Her Ip Address? In this article we will learn to get Ip Address of visitor using his Ip Address using ip-apiin PHP.
Get Your Exact Location using ip Address with IP-API In PHP
There are so many APIs provides accurate Location from Ip Address but i would like to recommend ip-api API. I am using ip-api service for more than a year and i have never face any issues. It’s Accurate, Provides well support and Never have downtime. ip-api is free for daily number of requests you can purchase it’s premium Plan to get more requests.
1
2
3
|
index.php
classes/
--ip-api.php
|
Inject class/ip-api.php
into index.php
and call getLocation()
function. $location
variable holds location array includes City, State and Country of visitor.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<?php
require 'class/ip-api.php';
$objLocation = new Location();
$arrAddress = $objLocation->getLocation();
?>
<!DOCTYPE html>
<html>
<head>
<title>Get Address Using Ip Address</title>
</head>
<body>
<h2>Location</h2>
<?php if (!empty($arrAddress)) { ?>
<div>
City: <?=$arrAddress['city'] ?>
</div><div>
State: <?=$arrAddress['regionName'] ?>
</div><div>
Country: <?=$arrAddress['country'] ?>
</div>
<?php } else { ?>
<div>
<p>Could not find Location. Please try again later!</p>
</div>
<?php } ?>
</body>
</html>
|
Holds two PHP functions getIpAddres()
and getLocation()
. getIpAddres()
fuction returns the Ip Address of visitor and getLocation()
will send CURL request to get Current Address. We need Enable CURL
extension from PHP Config.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
<?php
class Location
{
public function getLocation(){
$ipAddress = $this->getIpAddress();
$host = 'http://ip-api.com/php/'.$ipAddress;
try{
$ch = curl_init();
}catch(Exception $e){
return null;
}
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curlResponse = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$response = array();
if (!empty($curlResponse)) {
$curlResponse = unserialize($curlResponse);
$response['country'] = $curlResponse['country'];
$response['city'] = $curlResponse['city'];
$response['regionName'] = $curlResponse['regionName'];
}
return $response;
}
private function getIpAddress(){
if (isset($_SERVER['HTTP_CLIENT_IP'])){
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
} else if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else if (isset($_SERVER['HTTP_X_FORWARDED'])){
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
} else if (isset($_SERVER['HTTP_FORWARDED_FOR'])){
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
} else if (isset($_SERVER['HTTP_FORWARDED'])){
$ipaddress = $_SERVER['HTTP_FORWARDED'];
} else {
$ipaddress = $_SERVER['REMOTE_ADDR'];
}
return $ipaddress;
}
}
|
Now, run index.php
into web browser. It will fetch all the details of location and show on the screen.
Read More:
Composer – Dependency manager for PHP
Create a RESTful Web Service API with Slim
How To Integrate Stripe Payment Gateway Using PHP and JavaScript
sFileTypeChecker – JQuery Plugin To Validate Input File
sSwitch – JQuery Toggle Button Plugin For Sliding Toggle Switches