Skip to content

Commit

Permalink
Updated getVisitorIp function.
Browse files Browse the repository at this point in the history
  • Loading branch information
ip2location committed Sep 21, 2022
1 parent 79ff166 commit 201b614
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 77 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ Below is the description of the functions available in the **Database** class.
|**string** getDatabaseVersion()|Return the version of database|
|**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.|
|**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.|
|**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.|


### WebService Class
Expand Down Expand Up @@ -85,6 +84,7 @@ Below is the description of the functions available in the **IpTools** class.
| **array** cidrToIpv6($cidr) | Convert IPv6 CIDR notation into a list of IPv6 addresses. |
| **string** compressIpv6($ipv6) | Compress a IPv6 to shorten the length. |
| **string** expandIpv6($ipv6) | Expand a shorten IPv6 to full length. |
| **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.|

### Country Class

Expand Down
77 changes: 1 addition & 76 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Database
*
* @var string
*/
public const VERSION = '9.5.1';
public const VERSION = '9.5.2';

/**
* Unsupported field message.
Expand Down Expand Up @@ -1355,81 +1355,6 @@ public function getCidr($ip)
return false;
}

/**
* Get visitor real IP address.
*
* @param array $ipData
*
* @return string
* */
public function getVisitorIp(&$ipData = null)
{
$ip = $ipRemoteAdd = $ipSucuri = $ipIncap = $ipCf = $ipReal = $ipForwarded = $ipForwardedOri = '::1';

if (isset($_SERVER['REMOTE_ADDR']) && filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP)) {
$ipRemoteAdd = $ip = $_SERVER['REMOTE_ADDR'];
}

// Get real client IP if they are behind Sucuri firewall.
if (isset($_SERVER['HTTP_X_SUCURI_CLIENTIP']) && filter_var($_SERVER['HTTP_X_SUCURI_CLIENTIP'], FILTER_VALIDATE_IP)) {
$ipSucuri = $ip = $_SERVER['HTTP_X_SUCURI_CLIENTIP'];
}

// Get real client IP if they are behind Incapsula firewall.
if (isset($_SERVER['HTTP_INCAP_CLIENT_IP']) && filter_var($_SERVER['HTTP_INCAP_CLIENT_IP'], FILTER_VALIDATE_IP)) {
$ipIncap = $ip = $_SERVER['HTTP_INCAP_CLIENT_IP'];
}

// Get real client IP if they are behind CloudFlare protection.
if (isset($_SERVER['HTTP_CF_CONNECTING_IP']) && filter_var($_SERVER['HTTP_CF_CONNECTING_IP'], FILTER_VALIDATE_IP)) {
$ipCf = $ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
}

if (isset($_SERVER['HTTP_X_REAL_IP']) && filter_var($_SERVER['HTTP_X_REAL_IP'], FILTER_VALIDATE_IP)) {
$ipReal = $ip = $_SERVER['HTTP_X_REAL_IP'];
}

// Get real client IP if they are behind proxy server.
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ipForwardedOri = $_SERVER['HTTP_X_FORWARDED_FOR'];
$xip = trim(current(explode(',', $ipForwardedOri)));

if (filter_var($xip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
$ipForwarded = $ip = $xip;
}
}

if (!empty($ipData)) {
if (is_array($ipData)) {
if ($ipRemoteAdd != '::1') {
$ipData['REMOTE_ADDR'] = $ipRemoteAdd;
}

if ($ipSucuri != '::1') {
$ipData['HTTP_X_SUCURI_CLIENTIP'] = $ipSucuri;
}

if ($ipIncap != '::1') {
$ipData['HTTP_INCAP_CLIENT_IP'] = $ipIncap;
}

if ($ipCf != '::1') {
$ipData['HTTP_CF_CONNECTING_IP'] = $ipCf;
}

if ($ipReal != '::1') {
$ipData['HTTP_X_REAL_IP'] = $ipReal;
}

if ($ipForwardedOri != '::1') {
$ipData['HTTP_X_FORWARDED_FOR'] = $ipForwardedOri;
}
}
}

return $ip;
}

/**
* Get maximum size of a net block.
*
Expand Down
68 changes: 68 additions & 0 deletions src/IpTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,74 @@ public function expandIpv6($ipv6)
return implode(':', str_split($hex[0], 4));
}

public function getVisitorIp(&$ipData = null)
{
$ip = $ipRemoteAdd = $ipSucuri = $ipIncap = $ipCf = $ipReal = $ipForwarded = $ipForwardedOri = '::1';

if (isset($_SERVER['REMOTE_ADDR']) && filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP)) {
$ipRemoteAdd = $ip = $_SERVER['REMOTE_ADDR'];
}

// Get real client IP if they are behind Sucuri firewall.
if (isset($_SERVER['HTTP_X_SUCURI_CLIENTIP']) && filter_var($_SERVER['HTTP_X_SUCURI_CLIENTIP'], FILTER_VALIDATE_IP)) {
$ipSucuri = $ip = $_SERVER['HTTP_X_SUCURI_CLIENTIP'];
}

// Get real client IP if they are behind Incapsula firewall.
if (isset($_SERVER['HTTP_INCAP_CLIENT_IP']) && filter_var($_SERVER['HTTP_INCAP_CLIENT_IP'], FILTER_VALIDATE_IP)) {
$ipIncap = $ip = $_SERVER['HTTP_INCAP_CLIENT_IP'];
}

// Get real client IP if they are behind CloudFlare protection.
if (isset($_SERVER['HTTP_CF_CONNECTING_IP']) && filter_var($_SERVER['HTTP_CF_CONNECTING_IP'], FILTER_VALIDATE_IP)) {
$ipCf = $ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
}

if (isset($_SERVER['HTTP_X_REAL_IP']) && filter_var($_SERVER['HTTP_X_REAL_IP'], FILTER_VALIDATE_IP)) {
$ipReal = $ip = $_SERVER['HTTP_X_REAL_IP'];
}

// Get real client IP if they are behind proxy server.
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ipForwardedOri = $_SERVER['HTTP_X_FORWARDED_FOR'];
$xip = trim(current(explode(',', $ipForwardedOri)));

if (filter_var($xip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
$ipForwarded = $ip = $xip;
}
}

if (!empty($ipData)) {
if (is_array($ipData)) {
if ($ipRemoteAdd != '::1') {
$ipData['REMOTE_ADDR'] = $ipRemoteAdd;
}

if ($ipSucuri != '::1') {
$ipData['HTTP_X_SUCURI_CLIENTIP'] = $ipSucuri;
}

if ($ipIncap != '::1') {
$ipData['HTTP_INCAP_CLIENT_IP'] = $ipIncap;
}

if ($ipCf != '::1') {
$ipData['HTTP_CF_CONNECTING_IP'] = $ipCf;
}

if ($ipReal != '::1') {
$ipData['HTTP_X_REAL_IP'] = $ipReal;
}

if ($ipForwardedOri != '::1') {
$ipData['HTTP_X_FORWARDED_FOR'] = $ipForwardedOri;
}
}
}

return $ip;
}

private function ip2Bin($ip)
{
if (($n = inet_pton($ip)) === false) {
Expand Down

0 comments on commit 201b614

Please sign in to comment.