Create your own binary IP library file _ PHP Tutorial

Source: Internet
Author: User
Create a binary IP library file. I. preface all friends who have done IP address attribution query should have heard of the pure IP Address Library. the pure IP address library query is similar to this: if you only need to search for the user's home text based on the IP address, then I. Preface

Some friends who have done the IP address retrieval function should have heard of the pure IP Address Library. the pure IP address library query is similar to this:

If you only need to search for the user's home text based on the IP address and then display it, you only need to perform binary search according to the rules of the IP address Library and the display will be OK. (Detailed format)

But what if you need to obtain the geographic description based on the IP address and associate it with your existing administrative region data table?

Both of them can be implemented, but what about efficiency? Very poor! Especially for applications with high concurrency, both methods cannot be tested.

Why not use your own region ID to replace the region description of the pure IP database instead of the region description based on the data of the pure IP database (which can also be used by other IP libraries, what about creating a binary IP library file?

Let's go to the topic and see how to create a binary IP library file based on the pure IP database data.

Note: This article only describes the general idea and does not provide detailed Code. thank you.

2. preparations

We need to prepare two pieces of data:

  1. txt files extracted from the pure IP library.

A ip.exe tool will be available after the pure real IP library is downloaded, which can be generated by decompressing the above.

Generated Data 1-1. I have about 444290 records in this version.

    

-1

  2. your own provincial/municipal join data table.

There should be a lot of data on the internet. the table structure is similar to (area_id, area_level, area_name, area_pid), which represents the region ID, region level, region name, and parent region ID.

Of course, you can also use different structures without affecting our processing.

 

III. process

The data already exists. now let's plan the institution of the IP library that we need to generate.

From the title, we know that the IP database we need to generate is a binary data packet instead of a common text file. what is the structure of the IP database file?

:

  

We can see that our structure is as follows:

    • Header. It is located in the first 8 bytes of the file. The first 4 bytes are 32-bit integers. The value is the start position of the data part in the file; the last 4 bytes are also 32-bit integers. The value is the end position of the data part in the file.
    • Body data section. It consists of N fixed structs, each of which has 12 bytes and is an IP range data (ip_start, ip_end, area_code ). The three parts of the struct are also 32-bit signed integers, 4 bytes each. (If the area_code is small, it can contain 1 character)

  

The structure of the IP data packet has been fixed, and the next step is step-by-step processing.

    1. read the content of the IP text file one by one, convert the IP address to a 32-bit signed integer (custom ip2long), and obtain the final region from the regional text analysis.

A. rules for each line of the IP text file are as follows: the first 15 bytes are the IP address starting address, the last 15 bytes are the IP address ending address, and the last is the regional text description.

B. The converted IP address to a 32-bit signed integer occupies only 4 bytes, and solves the problem that the PHP function ip2long has different values in 32-bit and 64-bit systems. The new function is as follows:

 ip2Long32( = ('l', ('l', ( [1

Of course, you can also develop your own PHP extension, see here: http://www.cnblogs.com/iblaze/archive/2013/06/02/3112603.html

C. The region name (including province, city, county, and District) must be obtained. only countries are retained abroad.

      

  

    2. convert the obtained region information to the region ID.

    This part of processing is not very well described, because everyone may use different regions, but the general principle is to first find the ID based on the lowest level of region name (depending on the actual situation, it is possible to remove the city, county, and so on), if not, search for the upper level, such a cycle, until the region ID is obtained.

If no region ID is found, it is classified as unknown.

    3. compression. the compressed file is about 5.08 MB.

      Compression rule. The value in format corresponds to the type in pack:

      

Here, you must prompt that, because the IP address is converted to a 32-bit integer, the IP address after 128.0.0.0 will be negative, so you need to judge the negative number and put it in front of our IP database, after all, binary search is used and ordered data is required.

      

    4. search for IP addresses. use binary search,A maximum of data records can be searched 19 times,For example:

      

    

    4. for a single test, it seems that the speed is good.

      

5. Simple pressure test to check the effect

A. AB pressure test, using the local AB

B. test script on linux testing machine (General pc)

C. The stress test script is as follows:

      

D. pressure test statement: AB-n 10000-c 50 http: // 192.168.206.71/ipdata. php? Type = php

      

Good performance. Haha

      

It's over. how can I discuss it together? thank you ~

      

Some of my friends who have used the IP address retrieval function should have heard of the pure IP Address Library. the pure IP address library query is similar to this: if you only need to search for the user's plain text based on the IP address, then...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.