PHP Classes

IP2Location PHP Module: Get geographical details of an IP with IP2Location

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStar 53%Total: 1,163 All time: 3,259 This week: 129Up
Version License PHP version Categories
ip2location-php 7.0.0GNU Lesser Genera...3.0Networking, PHP 5, Geography
Description 

Author

This class can get geographical details of an IP with IP2Location.

It can read and parse an IP2Location data file made available in the IP2Location site, so it can locate a given IP address and get geographical details associated to the IP address range.

Currently it can extract details like the country, region, city, latitude, longitude, ZIP code, time zone, ISP, domain name, connection speed, IDD code, area code, weather station code, weather station name, mcc, mnc, mobile brand, and elevation.

This class can work with both the commercial and free versions of IP2Location database.

Picture of Chris
  Performance   Level  
Name: Chris <contact>
Classes: 2 packages by
Country: Malaysia Malaysia
Age: ???
All time rank: 17684 in Malaysia Malaysia
Week rank: 164 Up1 in Malaysia Malaysia Up

Recommendations

What is the best PHP geolocation class?
Find city and country name by IP

What is the best PHP visitor location class?
Find a visitor IP address longitude and latitude

What is the best PHP ip to latitude longitude class?
Given IP address return latitude longitude

Example

<?php
// Preset PHP settings
error_reporting(E_ALL);
ini_set('display_errors', 1);
set_time_limit(0);

require_once(
'ip2location.class.php');

// Standard lookup with no cache
$loc = new IP2Location('IP-COUNTRY-SAMPLE.BIN', IP2Location::FILE_IO);

/*
   Cache whole database into system memory and share among other scripts & websites
   WARNING: Please make sure your system have sufficient RAM to enable this feature
*/
//$loc = new IP2Location('databases/IP-COUNTRY-SAMPLE.BIN', IP2Location::SHARED_MEMORY);

/*
   Cache the database into memory to accelerate lookup speed
   WARNING: Please make sure your system have sufficient RAM to enable this feature
*/
//$loc = new IP2Location(ROOT . 'databases/IP-COUNTRY-SAMPLE.BIN', IP2Location::MEMORY_CACHE);

$ip = '8.8.8.8';

// Lookup for single field
echo 'Country Code: ' . $loc->lookup($ip, IP2Location::COUNTRY_CODE) . '<br />';
echo
'Country Name: ' . $loc->lookup($ip, IP2Location::COUNTRY_NAME) . '<br />';

// Lookup for all fields
$record = $loc->lookup($ip, IP2Location::ALL);

echo
'<pre>';
print_r($record);
echo
'</pre>';
?>


Details

IP2Location (PHP Module) ======================== 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, mcc, mnc, mobile brand, elevation, and usage type from IP address by using IP2Location database. This module uses a file based database available at IP2Location.com. This database simply contains IP blocks as keys, and other information such as country, region, city, latitude, longitude, ISP and domain name as values. This module can be used in many types of projects such as: 1) select the geographically closest mirror 2) analyze your web server logs to determine the countries of your visitors 3) credit card fraud detection 4) software export controls 5) display native language and currency 6) prevent password sharing and abuse of service 7) geotargeting in advertisement The database will be updated in monthly basis for the greater accuracy. Free sample database is available at /samples directory or download it from http://www.ip2location.com/developers.aspx. The free lite database is available at http://lite.ip2location.com. The complete database is available at http://www.ip2location.com under Premium subscription package. This module also support IP2Location IPv6 version. INSTALLATION To install this module, unzip the package and copy the following files to your web folder. i) ip2location.class.php ii) sample.php iii) databases/IP-COUNTRY-SAMPLE.BIN iv) databases/IPV6-COUNTRY.SAMPLE.BIN To test this installation, please browse sample.php using web browser. Please take note that this solution will works with the BIN database download from ip2location.com. DEPENDENCIES The LITE database is available at http://lite.ip2location.com for free download. The complete database is available at http://www.ip2location.com under subscription package. COPYRIGHT AND LICENCE Copyright (C) 2005-2013 by IP2Location.com, support@ip2location.com This library is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; If not, see <http://www.gnu.org/licenses/>.

  Files folder image Files (4)  
File Role Description
Accessible without login Plain text file BIN_DATABASE_README.txt Doc. BIN database download information
Plain text file ip2location.class.php Class ip2location class
Accessible without login Plain text file README.txt Doc. read me
Accessible without login Plain text file sample.php Example sample code

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:1,163
This week:0
All time:3,259
This week:129Up
User Ratings User Comments (2)
 All time
Utility:68%StarStarStarStar
Consistency:62%StarStarStarStar
Documentation:65%StarStarStarStar
Examples:59%StarStarStar
Tests:-
Videos:-
Overall:53%StarStarStar
Rank:2259
 
very good class.
8 years ago (Ashok kumar kashyap)
80%StarStarStarStarStar
Very simple to setup and use, thank you for sharing.
11 years ago (Terry Woody)
80%StarStarStarStarStar