Skip to content

Commit 201b614

Browse files
committed
Updated getVisitorIp function.
1 parent 79ff166 commit 201b614

File tree

3 files changed

+70
-77
lines changed

3 files changed

+70
-77
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ Below is the description of the functions available in the **Database** class.
5555
|**string** getDatabaseVersion()|Return the version of database|
5656
|**array** lookup($ip)|Return the IP information in array. Below is the information returned:<ul><li>ipNumber</li><li>ipVersion</li><li>ipAddress</li><li>countryCode</li><li>countryName</li><li>regionName</li><li>cityName</li><li>latitude</li><li>longitude</li><li>areaCode</li><li>iddCode</li><li>weatherStationCode</li><li>weatherStationName</li><li>mcc</li><li>mnc</li><li>mobileCarrierName</li><li>usageType</li><li>elevation</li><li>netSpeed</li><li>timeZone</li><li>zipCode</li><li>domainName</li><li>isp</li><li>addressType</li><li>category</li></ul>You can visit [IP2Location](https://www.ip2location.com/databases/db25-ip-country-region-city-latitude-longitude-zipcode-timezone-isp-domain-netspeed-areacode-weather-mobile-elevation-usagetype-addresstype-category) for the description of each field. Note: although the above names are not exactly matched with the names given in this link, but they are self-described.|
5757
|**array** getCidr($ip)|Return an array of the complete IP list in CIDR format of the detected row record based on the given IP address.|
58-
|**string** getVisitorIp()|Return the real IP address of the visitor. If an array of $ipData is supplied, it will return the list of IP address data found.|
5958

6059

6160
### WebService Class
@@ -85,6 +84,7 @@ Below is the description of the functions available in the **IpTools** class.
8584
| **array** cidrToIpv6($cidr) | Convert IPv6 CIDR notation into a list of IPv6 addresses. |
8685
| **string** compressIpv6($ipv6) | Compress a IPv6 to shorten the length. |
8786
| **string** expandIpv6($ipv6) | Expand a shorten IPv6 to full length. |
87+
| **string** getVisitorIp() | Return the real IP address of the visitor. If an array of $ipData is supplied, it will return the list of IP address data found.|
8888

8989
### Country Class
9090

src/Database.php

Lines changed: 1 addition & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Database
1212
*
1313
* @var string
1414
*/
15-
public const VERSION = '9.5.1';
15+
public const VERSION = '9.5.2';
1616

1717
/**
1818
* Unsupported field message.
@@ -1355,81 +1355,6 @@ public function getCidr($ip)
13551355
return false;
13561356
}
13571357

1358-
/**
1359-
* Get visitor real IP address.
1360-
*
1361-
* @param array $ipData
1362-
*
1363-
* @return string
1364-
* */
1365-
public function getVisitorIp(&$ipData = null)
1366-
{
1367-
$ip = $ipRemoteAdd = $ipSucuri = $ipIncap = $ipCf = $ipReal = $ipForwarded = $ipForwardedOri = '::1';
1368-
1369-
if (isset($_SERVER['REMOTE_ADDR']) && filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP)) {
1370-
$ipRemoteAdd = $ip = $_SERVER['REMOTE_ADDR'];
1371-
}
1372-
1373-
// Get real client IP if they are behind Sucuri firewall.
1374-
if (isset($_SERVER['HTTP_X_SUCURI_CLIENTIP']) && filter_var($_SERVER['HTTP_X_SUCURI_CLIENTIP'], FILTER_VALIDATE_IP)) {
1375-
$ipSucuri = $ip = $_SERVER['HTTP_X_SUCURI_CLIENTIP'];
1376-
}
1377-
1378-
// Get real client IP if they are behind Incapsula firewall.
1379-
if (isset($_SERVER['HTTP_INCAP_CLIENT_IP']) && filter_var($_SERVER['HTTP_INCAP_CLIENT_IP'], FILTER_VALIDATE_IP)) {
1380-
$ipIncap = $ip = $_SERVER['HTTP_INCAP_CLIENT_IP'];
1381-
}
1382-
1383-
// Get real client IP if they are behind CloudFlare protection.
1384-
if (isset($_SERVER['HTTP_CF_CONNECTING_IP']) && filter_var($_SERVER['HTTP_CF_CONNECTING_IP'], FILTER_VALIDATE_IP)) {
1385-
$ipCf = $ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
1386-
}
1387-
1388-
if (isset($_SERVER['HTTP_X_REAL_IP']) && filter_var($_SERVER['HTTP_X_REAL_IP'], FILTER_VALIDATE_IP)) {
1389-
$ipReal = $ip = $_SERVER['HTTP_X_REAL_IP'];
1390-
}
1391-
1392-
// Get real client IP if they are behind proxy server.
1393-
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
1394-
$ipForwardedOri = $_SERVER['HTTP_X_FORWARDED_FOR'];
1395-
$xip = trim(current(explode(',', $ipForwardedOri)));
1396-
1397-
if (filter_var($xip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
1398-
$ipForwarded = $ip = $xip;
1399-
}
1400-
}
1401-
1402-
if (!empty($ipData)) {
1403-
if (is_array($ipData)) {
1404-
if ($ipRemoteAdd != '::1') {
1405-
$ipData['REMOTE_ADDR'] = $ipRemoteAdd;
1406-
}
1407-
1408-
if ($ipSucuri != '::1') {
1409-
$ipData['HTTP_X_SUCURI_CLIENTIP'] = $ipSucuri;
1410-
}
1411-
1412-
if ($ipIncap != '::1') {
1413-
$ipData['HTTP_INCAP_CLIENT_IP'] = $ipIncap;
1414-
}
1415-
1416-
if ($ipCf != '::1') {
1417-
$ipData['HTTP_CF_CONNECTING_IP'] = $ipCf;
1418-
}
1419-
1420-
if ($ipReal != '::1') {
1421-
$ipData['HTTP_X_REAL_IP'] = $ipReal;
1422-
}
1423-
1424-
if ($ipForwardedOri != '::1') {
1425-
$ipData['HTTP_X_FORWARDED_FOR'] = $ipForwardedOri;
1426-
}
1427-
}
1428-
}
1429-
1430-
return $ip;
1431-
}
1432-
14331358
/**
14341359
* Get maximum size of a net block.
14351360
*

src/IpTools.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,74 @@ public function expandIpv6($ipv6)
263263
return implode(':', str_split($hex[0], 4));
264264
}
265265

266+
public function getVisitorIp(&$ipData = null)
267+
{
268+
$ip = $ipRemoteAdd = $ipSucuri = $ipIncap = $ipCf = $ipReal = $ipForwarded = $ipForwardedOri = '::1';
269+
270+
if (isset($_SERVER['REMOTE_ADDR']) && filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP)) {
271+
$ipRemoteAdd = $ip = $_SERVER['REMOTE_ADDR'];
272+
}
273+
274+
// Get real client IP if they are behind Sucuri firewall.
275+
if (isset($_SERVER['HTTP_X_SUCURI_CLIENTIP']) && filter_var($_SERVER['HTTP_X_SUCURI_CLIENTIP'], FILTER_VALIDATE_IP)) {
276+
$ipSucuri = $ip = $_SERVER['HTTP_X_SUCURI_CLIENTIP'];
277+
}
278+
279+
// Get real client IP if they are behind Incapsula firewall.
280+
if (isset($_SERVER['HTTP_INCAP_CLIENT_IP']) && filter_var($_SERVER['HTTP_INCAP_CLIENT_IP'], FILTER_VALIDATE_IP)) {
281+
$ipIncap = $ip = $_SERVER['HTTP_INCAP_CLIENT_IP'];
282+
}
283+
284+
// Get real client IP if they are behind CloudFlare protection.
285+
if (isset($_SERVER['HTTP_CF_CONNECTING_IP']) && filter_var($_SERVER['HTTP_CF_CONNECTING_IP'], FILTER_VALIDATE_IP)) {
286+
$ipCf = $ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
287+
}
288+
289+
if (isset($_SERVER['HTTP_X_REAL_IP']) && filter_var($_SERVER['HTTP_X_REAL_IP'], FILTER_VALIDATE_IP)) {
290+
$ipReal = $ip = $_SERVER['HTTP_X_REAL_IP'];
291+
}
292+
293+
// Get real client IP if they are behind proxy server.
294+
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
295+
$ipForwardedOri = $_SERVER['HTTP_X_FORWARDED_FOR'];
296+
$xip = trim(current(explode(',', $ipForwardedOri)));
297+
298+
if (filter_var($xip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
299+
$ipForwarded = $ip = $xip;
300+
}
301+
}
302+
303+
if (!empty($ipData)) {
304+
if (is_array($ipData)) {
305+
if ($ipRemoteAdd != '::1') {
306+
$ipData['REMOTE_ADDR'] = $ipRemoteAdd;
307+
}
308+
309+
if ($ipSucuri != '::1') {
310+
$ipData['HTTP_X_SUCURI_CLIENTIP'] = $ipSucuri;
311+
}
312+
313+
if ($ipIncap != '::1') {
314+
$ipData['HTTP_INCAP_CLIENT_IP'] = $ipIncap;
315+
}
316+
317+
if ($ipCf != '::1') {
318+
$ipData['HTTP_CF_CONNECTING_IP'] = $ipCf;
319+
}
320+
321+
if ($ipReal != '::1') {
322+
$ipData['HTTP_X_REAL_IP'] = $ipReal;
323+
}
324+
325+
if ($ipForwardedOri != '::1') {
326+
$ipData['HTTP_X_FORWARDED_FOR'] = $ipForwardedOri;
327+
}
328+
}
329+
}
330+
331+
return $ip;
332+
}
333+
266334
private function ip2Bin($ip)
267335
{
268336
if (($n = inet_pton($ip)) === false) {

0 commit comments

Comments
 (0)