php - In a piece of code used to determine which city an IP address belongs to, why should negative values ​​be considered when writing a piece of code?
淡淡烟草味
淡淡烟草味 2017-05-16 13:11:08
0
1
797

Let’s start with the code. Its original source should be DZ! There is also such a paragraph in my old project that I recently read.
I am not talented, so I analyzed it piece by piece, and now I find that there is a place that I really can’t understand. :if($ipbegin < 0) $ipbegin += pow(2, 32); // Under what circumstances does $ipbegin<0 ???
Another question is: this function references a qqwry.dat (almost written as bat at first). Where is the original source of this file, such as the official website, documentation, and how to use it? ?
I checked Baidu Encyclopedia, there is only a brief introduction, but I still can’t find the answer I want.

 public function qqwry($ip='222.222.222.222'){
        //$fd->resource(24, stream)
        if(!$fd = @fopen("./Public/qqwry.dat", 'rb')) {
            return 'Invalid IP data file';
        }
        $ip = explode('.', $ip);
        //$ipNum=2079656996
        $ipNum = $ip[0] * 16777216 + $ip[1] * 65536 + $ip[2] * 256 + $ip[3];

        //Get the start and end positions of the IP address index. At this time, the output of the two variables is garbled (should be binary)
        if(!($DataBegin = fread($fd, 4)) || !($DataEnd = fread($fd, 4)) ){
            return;
        }

        // The unpack() function unpacks data from a binary string, L--unsigned long integer
        // implode — Join array elements with (to) a string
        @$ipbegin = implode('', unpack('L', $DataBegin));// 6153398

        //pow(x,y)--The pow() function returns x raised to the yth power.
        if($ipbegin < 0) $ipbegin += pow(2, 32); // Under what circumstances does $ipbegin<0 ???


        // implode — Join array elements with (to) a string
        @$ipend = implode('', unpack('L', $DataEnd));

        if($ipend < 0) $ipend += pow(2, 32);
        $ipAllNum = ($ipend - $ipbegin) / 7 + 1;

        $BeginNum = $ip2num = $ip1num = 0;
        $ipAddr1 = $ipAddr2 = '';
        $EndNum = $ipAllNum;

        while($ip1num > $ipNum || $ip2num < $ipNum) {
        //The following is a large section of dichotomy, so I won’t copy it
淡淡烟草味
淡淡烟草味

reply all(1)
某草草
  • IP is expressed in binary as 32bit, so int is also 32bit. Use numbers to store IP, which has better search efficiency and storage space

  • The first digit of
  • int represents positive and negative. So it’s normal to have negative numbers

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template