-
Notifications
You must be signed in to change notification settings - Fork 17
/
ipInfo.inc.php
187 lines (167 loc) · 4.53 KB
/
ipInfo.inc.php
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
*
* @author Tom Green <[email protected]>
* @link https://github.com/BeingTomGreen/IP-User-Location
* @license MIT - http://beingtomgreen.mit-license.org/
*
* A basic PHP wrapper for the integrating the IP based location into your application.
* Uses IP Info DB <http://ipinfodb.com/>
*
*/
class ipInfo {
// Holds the API Key
private $apiKey = null;
// Hold the format we want to return the data in
private $apiFormat = null;
// Hold the version of the API we are working with
private $apiVersion = null;
// Hold the API URL (should have trailing /)
private $apiURL = 'http://api.ipinfodb.com/';
/**
* __construct
*
* @param string $apiKey - Your API key
* @param string $format - The format we want to return the data in
* @param string $version - The version of the API we are working with
*
*/
function __construct($apiKey, $format = 'raw', $version = 'v3')
{
// Save the API key
$this->apiKey = $apiKey;
// Save the API format
$this->apiFormat = $format;
// Save the API version
$this->apiVersion = $version;
}
/**
* getCountry
*
* Returns country level location data
*
* @param string $ip - the users IP address
*
* @return string/false - data if we have it, otherwise false
*
*/
public function getCountry($ip)
{
return $this->execute($ip, 'ip-country');
}
/**
* getCity
*
* Returns city level location data
*
* @param string $ip - the users IP address
*
* @return string/false - data if we have it, otherwise false
*
*/
public function getCity($ip)
{
return $this->execute($ip, 'ip-city');
}
/**
* execute
*
* Makes the specified CURL request - this is the meat of the class!
*
* @param string $ip - The users IP address
* @param string $endpoint - The API endpoint we wish to query
*
* @return string/bool - data if we have it, otherwise false
*
*/
private function execute($ip, $endpoint)
{
// Invalid IP address - make a note of it and return false
if ($this->validIP($ip) == false)
{
error_log('Invalid IP Address '. $ip);
return false;
}
// Build the URL
$url = $this->apiURL . $this->apiVersion .'/'. $endpoint .'/?key='. $this->apiKey .'&ip='. $ip .'&format='. $this->apiFormat;
// Initialise CURL
$handle = curl_init();
// Set the CURL options we need
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
// Grab the data
$data = curl_exec($handle);
// Grab the CURL error code and message as well as the HTTP Code
$errorCode = curl_errno($handle);
$errorMessage = curl_error($handle);
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
// Close the CURL connection
curl_close($handle);
// Check that we got a good HTTP response code
if ($httpCode == '200')
{
// Check our CURL error code is 0 (0 means OK!)
if ($errorCode == 0)
{
// Return the data
return $data;
}
// Curl Error
else
{
error_log('CURL error: '. $errorMessage .' (URL: '. $url .').');
return false;
}
}
// Bad HTTP response code
else
{
error_log('Bad HTTP response: '. $httpCode .' (URL: '. $url .').');
return false;
}
}
/**
* validIP
*
* Checks that the specified string is a valid IP Address
*
* @param string $ip - The IP Address
*
* @return bool - is the IP Address valid or not?
*
*/
public function validIP($ip)
{
return filter_var($ip, FILTER_VALIDATE_IP);
}
/**
* getIpAddress
*
* Returns the users IP Address
* This data shouldn't be trusted. Faking HTTP headers is trivial.
*
* @return string/false - the users IP address or false
*
*/
public function getIPAddress()
{
// Try REMOTE_ADDR
if (isset($_SERVER['REMOTE_ADDR']) and $_SERVER['REMOTE_ADDR'] != '')
{
return $_SERVER['REMOTE_ADDR'];
}
// Fall back to HTTP_CLIENT_IP
elseif (isset($_SERVER['HTTP_CLIENT_IP']) and $_SERVER['HTTP_CLIENT_IP'] != '')
{
return $_SERVER['HTTP_CLIENT_IP'];
}
// Finally fall back to HTTP_X_FORWARDED_FOR
// I'm aware this can sometimes pass the users LAN IP, but it is a last ditch attempt
elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) and $_SERVER['HTTP_X_FORWARDED_FOR'] != '')
{
return $_SERVER['HTTP_X_FORWARDED_FOR'];
}
// Nothing? Return false
return false;
}
}