PHP Classes

File: sample.php

Recommend this page to a friend!
  Classes of Chris   IP2Location PHP Module   sample.php   Download  
File: sample.php
Role: Example script
Content type: text/plain
Description: sample code
Class: IP2Location PHP Module
Get geographical details of an IP with IP2Location
Author: By
Last change: Fixed the required_once filename to ip2location.class.php
Updated the database path pointing to the same folder of sample file.
Date: 10 years ago
Size: 1,126 bytes
 

Contents

Class file image Download
<?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>';
?>