Skip to content

Commit e84153c

Browse files
committed
Added support for district, ASN and AS
1 parent fd1879e commit e84153c

File tree

3 files changed

+149
-26
lines changed

3 files changed

+149
-26
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ IP2Location (PHP Module)
55

66
*This is the official release maintained by IP2Location.com*
77

8-
This PHP module provides fast lookup of country, region, city, latitude, longitude, ZIP code, time zone, ISP, domain name, connection speed, IDD code, area code, weather station code, weather station name, MNC, MCC, mobile brand, elevation, usage type, address type and IAB category from IP address by using IP2Location database. This module uses a file based database available at IP2Location.com.
8+
This PHP module provides fast lookup of country, region, district, city, latitude, longitude, ZIP code, time zone, ISP, domain name, connection speed, IDD code, area code, weather station code, weather station name, MNC, MCC, mobile brand, elevation, usage type, address type, IAB category and ASN from IP address by using IP2Location database. This module uses a file based database available at IP2Location.com.
99

1010
This module can be used in many types of projects such as:
1111

@@ -50,10 +50,10 @@ Below is the description of the functions available in the **Database** class.
5050
|---|---|
5151
|Constructor|Expect 2 input parameters:<ol><li>Full path of IP2Location BIN data file.</li><li>File Open Mode<ul><li> SHARED_MEMORY</li><li>MEMORY_CACHE</li><li>FILE_IO</li></ul></li></ol>For SHARED_MEMORY and MEMORY_CACHE, it will require your server to have sufficient memory to hold the BIN data, otherwise it will raise the errors during the object initialization.|
5252
|**string** getDate()|Return the database's compilation date as a string of the form 'YYYY-MM-DD'|
53-
|**string** getType()|Return the database's type, 1 to 25 respectively for DB1 to DB25. Please visit https://www.ip2location.com/databases for details.|
53+
|**string** getType()|Return the database's type, 1 to 26 respectively for DB1 to DB26. Please visit https://www.ip2location.com/databases for details.|
5454
|**string** getModuleVersion()|Return the version of module|
5555
|**string** getDatabaseVersion()|Return the version of database|
56-
|**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.|
56+
|**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><li>district</li><li>asn</li><li>as</li></ul>You can visit [IP2Location](https://www.ip2location.com/databases/db26-ip-country-region-city-latitude-longitude-zipcode-timezone-isp-domain-netspeed-areacode-weather-mobile-elevation-usagetype-addresstype-category-district-asn) 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.|
5858

5959

examples/example.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
echo 'ISP Name : ' . $records['isp'] . "\n";
4848
echo 'Address Type : ' . $records['addressType'] . "\n";
4949
echo 'Category : ' . $records['category'] . "\n";
50+
echo 'District : ' . $records['district'] . "\n";
51+
echo 'ASN : ' . $records['asn'] . "\n";
52+
echo 'AS : ' . $records['as'] . "\n";
5053
echo '</pre>';
5154

5255
echo '<pre>

src/Database.php

Lines changed: 143 additions & 23 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.6.1';
15+
public const VERSION = '9.7.0';
1616

1717
/**
1818
* Unsupported field message.
@@ -203,6 +203,27 @@ class Database
203203
*/
204204
public const CATEGORY = 22;
205205

206+
/**
207+
* District.
208+
*
209+
* @var int
210+
*/
211+
public const DISTRICT = 23;
212+
213+
/**
214+
* ASN.
215+
*
216+
* @var int
217+
*/
218+
public const ASN = 24;
219+
220+
/**
221+
* AS.
222+
*
223+
* @var int
224+
*/
225+
public const AS = 25;
226+
206227
/**
207228
* Country name and code.
208229
*
@@ -394,28 +415,31 @@ class Database
394415
* @var array
395416
*/
396417
private $columns = [
397-
self::COUNTRY_CODE => [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8],
398-
self::COUNTRY_NAME => [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8],
399-
self::REGION_NAME => [0, 0, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12],
400-
self::CITY_NAME => [0, 0, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16],
401-
self::LATITUDE => [0, 0, 0, 0, 20, 20, 0, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
402-
self::LONGITUDE => [0, 0, 0, 0, 24, 24, 0, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24],
403-
self::ZIP_CODE => [0, 0, 0, 0, 0, 0, 0, 0, 28, 28, 28, 28, 0, 28, 28, 28, 0, 28, 0, 28, 28, 28, 0, 28, 28],
404-
self::TIME_ZONE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 32, 28, 32, 32, 32, 28, 32, 0, 32, 32, 32, 0, 32, 32],
405-
self::ISP => [0, 12, 0, 20, 0, 28, 20, 28, 0, 32, 0, 36, 0, 36, 0, 36, 0, 36, 28, 36, 0, 36, 28, 36, 36],
406-
self::DOMAIN_NAME => [0, 0, 0, 0, 0, 0, 24, 32, 0, 36, 0, 40, 0, 40, 0, 40, 0, 40, 32, 40, 0, 40, 32, 40, 40],
407-
self::NET_SPEED => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 44, 0, 44, 32, 44, 0, 44, 0, 44, 0, 44, 44],
408-
self::IDD_CODE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 48, 0, 48, 0, 48, 36, 48, 0, 48, 48],
409-
self::AREA_CODE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 52, 0, 52, 0, 52, 40, 52, 0, 52, 52],
410-
self::WEATHER_STATION_CODE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 56, 0, 56, 0, 56, 0, 56, 56],
411-
self::WEATHER_STATION_NAME => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 60, 0, 60, 0, 60, 0, 60, 60],
412-
self::MCC => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 64, 0, 64, 36, 64, 64],
413-
self::MNC => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 68, 0, 68, 40, 68, 68],
414-
self::MOBILE_CARRIER_NAME => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 44, 72, 0, 72, 44, 72, 72],
415-
self::ELEVATION => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 44, 76, 0, 76, 76],
416-
self::USAGE_TYPE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48, 80, 80],
417-
self::ADDRESS_TYPE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 84],
418-
self::CATEGORY => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 88],
418+
self::COUNTRY_CODE => [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8],
419+
self::COUNTRY_NAME => [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8],
420+
self::REGION_NAME => [0, 0, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12],
421+
self::CITY_NAME => [0, 0, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16],
422+
self::LATITUDE => [0, 0, 0, 0, 20, 20, 0, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
423+
self::LONGITUDE => [0, 0, 0, 0, 24, 24, 0, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24],
424+
self::ZIP_CODE => [0, 0, 0, 0, 0, 0, 0, 0, 28, 28, 28, 28, 0, 28, 28, 28, 0, 28, 0, 28, 28, 28, 0, 28, 28, 28],
425+
self::TIME_ZONE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 32, 28, 32, 32, 32, 28, 32, 0, 32, 32, 32, 0, 32, 32, 32],
426+
self::ISP => [0, 12, 0, 20, 0, 28, 20, 28, 0, 32, 0, 36, 0, 36, 0, 36, 0, 36, 28, 36, 0, 36, 28, 36, 36, 36],
427+
self::DOMAIN_NAME => [0, 0, 0, 0, 0, 0, 24, 32, 0, 36, 0, 40, 0, 40, 0, 40, 0, 40, 32, 40, 0, 40, 32, 40, 40, 40],
428+
self::NET_SPEED => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 44, 0, 44, 32, 44, 0, 44, 0, 44, 0, 44, 44, 44],
429+
self::IDD_CODE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 48, 0, 48, 0, 48, 36, 48, 0, 48, 48, 48],
430+
self::AREA_CODE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 52, 0, 52, 0, 52, 40, 52, 0, 52, 52, 52],
431+
self::WEATHER_STATION_CODE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 56, 0, 56, 0, 56, 0, 56, 56, 56],
432+
self::WEATHER_STATION_NAME => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 60, 0, 60, 0, 60, 0, 60, 60, 60],
433+
self::MCC => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 64, 0, 64, 36, 64, 64, 64],
434+
self::MNC => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 68, 0, 68, 40, 68, 68, 68],
435+
self::MOBILE_CARRIER_NAME => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 44, 72, 0, 72, 44, 72, 72, 72],
436+
self::ELEVATION => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 44, 76, 0, 76, 76, 76],
437+
self::USAGE_TYPE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48, 80, 80, 80],
438+
self::ADDRESS_TYPE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 84, 84],
439+
self::CATEGORY => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 88, 88],
440+
self::DISTRICT => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 92],
441+
self::ASN => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96],
442+
self::AS => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100],
419443
];
420444

421445
/**
@@ -446,6 +470,9 @@ class Database
446470
self::USAGE_TYPE => 'usageType',
447471
self::ADDRESS_TYPE => 'addressType',
448472
self::CATEGORY => 'category',
473+
self::DISTRICT => 'district',
474+
self::ASN => 'asn',
475+
self::AS => 'as',
449476
self::IP_ADDRESS => 'ipAddress',
450477
self::IP_VERSION => 'ipVersion',
451478
self::IP_NUMBER => 'ipNumber',
@@ -458,6 +485,7 @@ class Database
458485
*/
459486
private $databases = [
460487
// IPv4 databases
488+
'IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-ADDRESSTYPE-CATEGORY-DISTRICT-ASN',
461489
'IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-ADDRESSTYPE-CATEGORY',
462490
'IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE',
463491
'IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ISP-DOMAIN-MOBILE-USAGETYPE',
@@ -485,6 +513,7 @@ class Database
485513
'IP-COUNTRY',
486514

487515
// IPv6 databases
516+
'IPV6-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-ADDRESSTYPE-CATEGORY-DISTRICT-ASN',
488517
'IPV6-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-ADDRESSTYPE-CATEGORY',
489518
'IPV6-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE',
490519
'IPV6-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ISP-DOMAIN-MOBILE-USAGETYPE',
@@ -968,6 +997,10 @@ public function lookup($ip, $fields = null, $asNamed = true)
968997
// Get IP version and number
969998
list($ipVersion, $ipNumber) = $this->ipVersionAndNumber($ip);
970999

1000+
if (!$ipVersion) {
1001+
return false;
1002+
}
1003+
9711004
// Perform a binary search
9721005
$pointer = $this->binSearch($ipVersion, $ipNumber);
9731006

@@ -1003,6 +1036,9 @@ public function lookup($ip, $fields = null, $asNamed = true)
10031036
$ifields[] = self::USAGE_TYPE;
10041037
$ifields[] = self::ADDRESS_TYPE;
10051038
$ifields[] = self::CATEGORY;
1039+
$ifields[] = self::DISTRICT;
1040+
$ifields[] = self::ASN;
1041+
$ifields[] = self::AS;
10061042

10071043
$ifields[] = self::COUNTRY;
10081044
$ifields[] = self::COORDINATES;
@@ -1046,6 +1082,9 @@ public function lookup($ip, $fields = null, $asNamed = true)
10461082
self::USAGE_TYPE => false,
10471083
self::ADDRESS_TYPE => false,
10481084
self::CATEGORY => false,
1085+
self::DISTRICT => false,
1086+
self::ASN => false,
1087+
self::AS => false,
10491088
self::COUNTRY => false,
10501089
self::COORDINATES => false,
10511090
self::IDD_AREA => false,
@@ -1264,6 +1303,27 @@ public function lookup($ip, $fields = null, $asNamed = true)
12641303
}
12651304
break;
12661305

1306+
case self::DISTRICT:
1307+
if (!$done[self::DISTRICT]) {
1308+
$results[self::DISTRICT] = $this->readDistrict($pointer);
1309+
$done[self::DISTRICT] = true;
1310+
}
1311+
break;
1312+
1313+
case self::ASN:
1314+
if (!$done[self::ASN]) {
1315+
$results[self::ASN] = $this->readAsn($pointer);
1316+
$done[self::ASN] = true;
1317+
}
1318+
break;
1319+
1320+
case self::AS:
1321+
if (!$done[self::AS]) {
1322+
$results[self::AS] = $this->readAs($pointer);
1323+
$done[self::AS] = true;
1324+
}
1325+
break;
1326+
12671327
case self::IP_ADDRESS:
12681328
if (!$done[self::IP_ADDRESS]) {
12691329
$results[self::IP_ADDRESS] = $ip;
@@ -2037,6 +2097,66 @@ private function readCategory($pointer)
20372097
return $this->readString($this->columns[self::CATEGORY][$this->type]);
20382098
}
20392099

2100+
/**
2101+
* High level function to fetch the district.
2102+
*
2103+
* @param int $pointer Position to read from, if false, return self::INVALID_IP_ADDRESS
2104+
*
2105+
* @return string
2106+
*/
2107+
private function readDistrict($pointer)
2108+
{
2109+
if ($pointer === false) {
2110+
return self::INVALID_IP_ADDRESS;
2111+
}
2112+
2113+
if ($this->columns[self::DISTRICT][$this->type] === 0) {
2114+
return self::FIELD_NOT_SUPPORTED;
2115+
}
2116+
2117+
return $this->readString($this->columns[self::DISTRICT][$this->type]);
2118+
}
2119+
2120+
/**
2121+
* High level function to fetch the ASN.
2122+
*
2123+
* @param int $pointer Position to read from, if false, return self::INVALID_IP_ADDRESS
2124+
*
2125+
* @return string
2126+
*/
2127+
private function readAsn($pointer)
2128+
{
2129+
if ($pointer === false) {
2130+
return self::INVALID_IP_ADDRESS;
2131+
}
2132+
2133+
if ($this->columns[self::ASN][$this->type] === 0) {
2134+
return self::FIELD_NOT_SUPPORTED;
2135+
}
2136+
2137+
return $this->readString($this->columns[self::ASN][$this->type]);
2138+
}
2139+
2140+
/**
2141+
* High level function to fetch the AS.
2142+
*
2143+
* @param int $pointer Position to read from, if false, return self::INVALID_IP_ADDRESS
2144+
*
2145+
* @return string
2146+
*/
2147+
private function readAs($pointer)
2148+
{
2149+
if ($pointer === false) {
2150+
return self::INVALID_IP_ADDRESS;
2151+
}
2152+
2153+
if ($this->columns[self::AS][$this->type] === 0) {
2154+
return self::FIELD_NOT_SUPPORTED;
2155+
}
2156+
2157+
return $this->readString($this->columns[self::AS][$this->type]);
2158+
}
2159+
20402160
/**
20412161
* Get the boundaries for an IP address.
20422162
*

0 commit comments

Comments
 (0)