Skip to content

Commit 79ff166

Browse files
committed
Added getVisitorIp function.
1 parent 6015891 commit 79ff166

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

LICENSE.TXT

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2017 IP2Location.com
3+
Copyright (c) 2022 IP2Location.com
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ 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.|
5859

5960

6061
### WebService Class
@@ -129,6 +130,6 @@ Below are the list of other framework library that you can install and use right
129130

130131
## COPYRIGHT AND LICENSE
131132

132-
Copyright (C) 2005-2021 by IP2Location.com
133+
Copyright (C) 2005-2022 by IP2Location.com
133134

134135
License under MIT

src/Database.php

Lines changed: 76 additions & 1 deletion
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.0';
15+
public const VERSION = '9.5.1';
1616

1717
/**
1818
* Unsupported field message.
@@ -1355,6 +1355,81 @@ 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+
13581433
/**
13591434
* Get maximum size of a net block.
13601435
*

0 commit comments

Comments
 (0)