Login   Register  
PHP Classes
elePHPant
Icontem

Didn't work.

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us

      Country From IP  >  All threads  >  Didn't work.  >  (Un) Subscribe thread alerts  
Subject:Didn't work.
Summary:Package rating comment
Messages:4
Author:Aaron
Date:2009-01-28 11:53:31
Update:2009-01-29 07:27:48
 

Aaron rated this package as follows:

Utility: Not sure
Consistency: Not sure
Documentation: Not sure
Examples: Not sure

  1. Didn't work.   Reply  
Picture of Aaron
Aaron
2009-01-28 11:53:31
Didn't work.

updated code:

<?php


class Geographical_Location {


public $files;
public $locationDB;
public $ip;


function __construct($ip) {
$this->files = array(
1 => _ROOT.DS.'scripts/geographical_locations/1.txt',
2 => _ROOT.DS.'scripts/geographical_locations/2.txt',
3 => _ROOT.DS.'scripts/geographical_locations/3.txt',
4 => _ROOT.DS.'scripts/geographical_locations/4.txt',
5 => _ROOT.DS.'scripts/geographical_locations/5.txt');
$this->locationDB = _ROOT.DS.'scripts/geographical_locations/5.txt';
$this->ip = $ip;

$this->ValdateIP();
$this->GetCountryName();
}


public function ValdateIP() {

$ipArray = explode('.', $this->ip);

if (count($ipArray) != 4) {
echo "<font color='red' size='3'><b>ERROR: </b> Invalid IP</font>";
return false;
} else {
return true;
}
}


public function GetCountryName() {

$ip = $this->ip;
$ip = sprintf("%u", ip2long($ip));

foreach ($this->files as $key => $value) {
$csvArray = file($value);
for ($i=0; $i<count($csvArray); $i++) {
$arrayOfLine = explode(',', $csvArray[$i]);
if ($ip >= $arrayOfLine[0] && $ip <= $arrayOfLine[1] ) {
return $countryName = $arrayOfLine[2];
}
}
}
return false;
}


public function ReturnFlagPath() {

if ($countryName = trim(ucwords(strtolower($this->GetCountryName($this->ip))) )) {
$countryName = str_replace(' ', '%20', $countryName);
return "flag/$countryName.gif";
} else {
return false;
}
}


}


?>

  2. Re: Didn't work.   Reply  
Picture of Er. Rochak Chauhan
Er. Rochak Chauhan
2009-01-28 12:16:55 - In reply to message 1 from Aaron
I hope you do download the database in csv/txt format. If not download it before running this class. The link is provided in the class page.
alternatively you can use API to find county from IP for free.

Its called: GeoIP API Connector
More link: http://rochakchauhan.com/blog/2009/01/27/geoip-api-connector-get-geographic-location-of-ip-address-from-geoip/

Regards,
Rochak Chauhan.

  3. Re: Didn't work.   Reply  
Picture of Aaron
Aaron
2009-01-28 17:17:51 - In reply to message 2 from Er. Rochak Chauhan
I will look into it.


Do you like my changes?

  4. Re: Didn't work.   Reply  
Picture of Er. Rochak Chauhan
Er. Rochak Chauhan
2009-01-29 07:27:48 - In reply to message 3 from Aaron
Its great that you modified it. To be honest I was more focused on GeoIP API class.
Will update it soon.