Jump to content

Help with Country IP script


saud

Recommended Posts

Whenever I run this script I get wrong results for country.

all the database and files can be downloaded from http://software77.net/cgi-bin/ip-country/geo-ip.pl

But the Perl script gives the correct result. All the files are listed there.

 

 

If i change the $addr to "213.6.49.205" it shows wrong results and should show Norway.

<?php

 

// Get the surfer's ip address

$addr = getenv("REMOTE_ADDR");

echo "Your IP, IPv4: " . $addr . "<br />\n";

 

$ip = sprintf("%010u", ip2long($addr));

echo "Your IP, numerical: " . $ip . "<br /><br />\n";

 

// Initiate the timer

$time_start = microtime_float();

 

// Open the csv file for reading

$handle = fopen("IpToCountry.csv", "r");

 

// Load array with start ips

$row = 1;

while (($buffer = fgets($handle, 9096)) !== FALSE) {

  $array[$row] = $buffer;

  $row++;

}

 

// Time loading

$time_end = microtime_float();

$time = substr($time_end - $time_start, 0, 7);

echo "Array with " . $row . " start ips loaded in $time seconds<br /><br />\n";

 

// Locate the row with our ip using bisection

$row_lower = '0';

$row_upper = $row;

while (($row_upper - $row_lower) > 1) {

  $row_midpt = (int) (($row_upper + $row_lower) / 2);

  $buffer = $array[$row_midpt];

  $start_ip = sprintf("%010u", substr($buffer, 1, strpos($buffer, ",") - 1));

  if ($ip >= $start_ip) {

    $row_lower = $row_midpt;

  } else {

    $row_upper = $row_midpt;

  }

}

 

// Time locating

$time_end = microtime_float();

$time = substr($time_end - $time_start, 0, 7);

echo "Row with your ip (# " . $row_lower . ") located after $time seconds<br /><br />\n";

 

// Read the row with our ip

$buffer = $array[$row_lower];

$buffer = str_replace("\"", "", $buffer);

$ipdata = explode(",", $buffer);

echo "Data retrieved from the csv file:<br />\n";

echo "ipstart = " . sprintf("%010u", $ipdata[0]) . "<br />\n";

echo "ipend = " . sprintf("%010u", $ipdata[1]) . "<br />\n";

echo "registry = " . $ipdata[2] . "<br />\n";

echo "assigned = " . date('j.n.Y', $ipdata[3]) . "<br />\n";

echo "iso2 = " . $ipdata[4] . "<br />\n";

echo "iso3 = " . $ipdata[5] . "<br />\n";

echo "country = " . $ipdata[6] . "<br /><br />\n";

 

// Close the csv file

fclose($handle);

 

// Total execution time

$time_end = microtime_float();

$time = substr($time_end - $time_start, 0, 7);

echo "Executing the whole script took $time seconds\n";

 

function microtime_float()

{

  list($usec, $sec) = explode(" ", microtime());

  return ((float)$usec + (float)$sec);

}

 

?>

 

The thing is that IP is correctly mentioned in the database, just the PHP script is not reading the database correctly, while the CGI script is doing it correctly. DO a test yourself by downloading both the files, they are free too.

http://webnet77.com/scripts/geo-ip/IP-country-FREE.zip

 

Database download: http://software77.net/cgi-bin/ip-cou...ction=download

 

Main website: http://software77.net/cgi-bin/ip-country/geo-ip.pl

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.