Skip to content

Commit

Permalink
Added getVisitorIp function.
Browse files Browse the repository at this point in the history
  • Loading branch information
ip2location committed Sep 21, 2022
1 parent 6015891 commit 79ff166
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
2 changes: 1 addition & 1 deletion LICENSE.TXT
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 IP2Location.com
Copyright (c) 2022 IP2Location.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ 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 @@ -129,6 +130,6 @@ Below are the list of other framework library that you can install and use right

## COPYRIGHT AND LICENSE

Copyright (C) 2005-2021 by IP2Location.com
Copyright (C) 2005-2022 by IP2Location.com

License under MIT
77 changes: 76 additions & 1 deletion 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.0';
public const VERSION = '9.5.1';

/**
* Unsupported field message.
Expand Down Expand Up @@ -1355,6 +1355,81 @@ 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

0 comments on commit 79ff166

Please sign in to comment.