php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #30313 ip to country function
Submitted: 2004-10-02 21:12 UTC Modified: 2004-10-11 15:59 UTC
From: bakr_fathy at yahoo dot com Assigned:
Status: Closed Package: Website problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: bakr_fathy at yahoo dot com
New email:
PHP Version: OS:

 

 [2004-10-02 21:12 UTC] bakr_fathy at yahoo dot com
Description:
------------
I'm Bakr Alsharif from Egypt, I had to post this bug to you,
Because it's in a function called "i2c_go" which is being used in all php.net mirrors

The file is    : (./includes/ip-to-country.inc)
required files : (./backend/ip-to-country.idx) , (backend/ip-to-country.db)

Details :-
I was using the same function with the same files as descriped above, and when i tried some ips to get their countries' codes
the country was "NA" while the ip is already recorded in the "db" file.

For example try the following ip : {217.144.8.107} and you sould have the following results
so the country code should be JOR, But the function will return "NA"

Real IP = 217.144.8.107
Long IP = 3650095211
Record Number = 50661
Record Value  = 36500930563650097151JOR

When I debuged the function i got the problem.
First, the function "i2c_go" uses the "idx" file to speed the search , so it gets the start and the end
of the possible records in the "db" file by calling the "i2c_search_in_index" function.

When using the ip in the example the start and end will be 50616 : 51254

After getting those values the "i2c_go" function calls the "i2c_search_in_db" to start the real search in the "db" file.
that function "i2c_search_in_db" opens the "backend/ip-to-country.idx" file and sets the file position indicator
to the starting record returned by the "i2c_search_in_index" function, like this

    fseek($ipdb, $idx[0]*24);

the "$ipdb" is the db file handler and "$idx[0]" is the index start and "24" is number of bytes in each line.

In our case with the ip in the example it will set the file pointer to the END OF LINE "50661"
So when we use "fread" we will read from line "50662" NOT line "50661" and that will make us never find the right record
because we has just skipped it. So we land on "NA" instead of "JOR".

To Solve this problem , we should set the file pointer to the END of line "50660" so we can search from line "50661"
which returned from the i2c_search_in_index function.

The "fseek" code line should be
    fseek($ipdb, ($idx[0]-1)*24);
    
I hope everything was clear.

Thanks for your time,

Bakr Alsharif
http://systurn.com
bakr_fathy@yahoo.com
++20127809973




Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-10-03 21:49 UTC] bakr_fathy at yahoo dot com
Sorry, but function "i2c_search_in_db"
opens the "backend/ip-to-country.db" file
NOT "backend/ip-to-country.idx".
 [2004-10-11 15:59 UTC] goba@php.net
I have committed a modified version of your patch to CVS.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Thu Jun 18 22:00:01 2026 UTC