PHP Classes

Faster PHP IP2Location: Faster method to find the location of IP addresses

Recommend this page to a friend!
  Info   Example   Screenshots   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 66%Total: 556 All time: 5,474 This week: 78Up
Version License PHP version Categories
faster-ip2location 1.0.1GNU Lesser Genera...5Algorithms, PHP 5, Geography
Description 

Author

This class implements a faster method to find the location of IP addresses.

It can take a IP2Location database file and generates list files that are faster to search.

The class uses the data in those list files to lookup for the location a given IP addresses.

Innovation Award
PHP Programming Innovation award nominee
July 2016
Number 3


Prize: SourceGuarding PHP encoder tool
IP2Location is a well known service that uses database files to determine the location of IP addresses of given ranges.

Usually it uses the binary search method to find the location of a given IP address, so it reduces the amount of information to be read from IP2Location database file.

The class implements a more optimized method to lookup the location of IP address in pure PHP code. It departs from a database file from IP2Location more efficient data structures to lookup.

Manuel Lemos
Picture of Chi H.
  Performance   Level  
Name: Chi H. <contact>
Classes: 28 packages by
Country: France France
Age: 39
All time rank: 33922 in France France
Week rank: 197 Up10 in France France Up
Innovation award
Innovation award
Nominee: 22x

Winner: 3x

Example

<?php
require_once 'IP2Location.php';

error_reporting(E_ALL);
echo
ini_get("memory_limit")."\n";
ini_set('max_execution_time', 0);

//Timer started
$m_start = microtime(true);

//Load file using FILE_IO method for testing
//$db = new \IP2Location\Database('./databases/IP2LOCATION-LITE-DB11.BIN', \IP2Location\Database::FILE_IO);
$db = new \IP2Location\Database('./databases/IP2LOCATION-LITE-DB11.IPV6.BIN', \IP2Location\Database::FILE_IO);

//Get IP address for testing
$ip_list = explode("\r\n", file_get_contents('ip_list.txt'));

//Run the testing
$ip_data = array();
foreach(
$ip_list as $ip){
       
//get all records
       
$records = $db->lookup($ip, \IP2Location\Database::ALL);

       
array_push($ip_data, array('ipaddr'=>$records['ipAddress'],
                                  
'countryCode'=>$records['countryCode'],
                                  
'regionName'=>$records['regionName'],
                                   
'cityName'=>$records['cityName'],
                                    
'lat'=>$records['latitude'],
                                     
'long'=>$records['longitude']
                                    ));
}

//Timer stopped
$m_end = microtime(true);

//Get the expected result data for validation
$ip_result = explode("\r\n", file_get_contents('ip_result.txt'));
if (
count($ip_data) != count($ip_result)){
        echo
'The ip list and result count was not tally. Please recheck your code.' . "\n";
        return;
}
else{
       
//Validate the result
       
$idx=0;
        foreach(
$ip_result as $row){
               
$row_data = explode("\t", $row);
                if (
$row_data[0] != $ip_data[$idx]['countryCode'] || $row_data[1] != $ip_data[$idx]['regionName'] || $row_data[2] != $ip_data[$idx]['cityName']){
                        echo
'Error found at record #' . ($idx+1) . "\n";
                       
//return;
               
}

               
$idx+=1;
        }
}

echo
memory_get_usage()."\n";

//Output the time taken (Note: Time taken not including the time used for result validation)
$time_taken = $m_end - $m_start;
echo
'Time taken: ' . $time_taken . ' seconds' . "\n";
file_put_contents('testing_report.txt', 'Time taken: ' . $time_taken . ' seconds' . "\n");
?>


Details

Better IP2Location PHP Library

  1. What is does it do? Better IP2Location PHP Library for faster query time.

1.2. Features - Faster query time

  1. Requirements -PHP 5 -IP2Location lite database (DB11)
  2. Installation Download test.php, IP2Location.php, ip_list.txt, ip_result.txt and free IP2Location Lite DB 11 to a folder on your pc. Create inside the main folder antother folder "databases" and move the IP2LOCATION-LITE-DB11.BIN file into it.
  3. Tutorial 4.1 run the "test.php" in the command shell with option -f: php -f test.php. 4.2 Pre-calculation of binary search 4.2.1 Download calc.php, IP2LocationCalc.php to the main folder 4.2.2 Create pre-calculation and run calc.php in command shell (php -d calc.php) and wait 30-50min. 4.2.3 Open the file1.txt and copy its content to the file IP2Location.php into var $k replacing the old array. 4.3 Alternative solution (slower) 4.3.1 Replace in test.php require_once 'IP2Location.php'; with require_once 'IP2LocationCalc.php'; 4.3.2 run test.php (php -f test.php)
  4. Changelog 01.06.2016 Initial release

Screenshots (2)  
  • improvement
  • winner
  Files folder image Files (10)  
File Role Description
Plain text file calc.php Class Class source
Accessible without login Plain text file file1.txt Output Sample output
Plain text file IP2Location.php Class Class source
Plain text file IP2LocationCalc.php Class Class source
Accessible without login Plain text file ip_list.txt Data Auxiliary data
Accessible without login Plain text file ip_result.txt Data Auxiliary data
Accessible without login Plain text file Readme.md Doc. Documentation
Accessible without login Plain text file test.php Example Example script

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  
 80%
Total:556
This week:0
All time:5,474
This week:78Up
 User Ratings  
 
 All time
Utility:87%StarStarStarStarStar
Consistency:75%StarStarStarStar
Documentation:81%StarStarStarStarStar
Examples:75%StarStarStarStar
Tests:-
Videos:-
Overall:66%StarStarStarStar
Rank:475