PHP转换IP地址到真实地址
生在红旗下长在春风里,长期浸泡在河蟹社会里面所以久而久之就有了一些河蟹的思维方式,正好有一段时间在做一个.NET的网站访问统计系统,顺便想着怎么“监视”下每一个留言的博主们的所在地,于是就有了如今下图所示的根据获取到的留言者的IP地址得到留言者所在的地区,当然并没有河蟹社会监视人民群众的意思,纯属了解一下各位博主所在的真实地点,万一是个美女博主不就可以让我有线索可循。
想要把IPv4地址转为真实的地址,肯定要参考IP数据库,商业的IP数据库存储在关系型数据库中,查询和使用都非常方便,但是成本不是个人和小公司愿意承受的,所以简单应用的思路就是利用一些免费的IP数据库或者一些大网站提供的查询API,他们的数据量足够我们使用了。
1. 利用纯真IP数据库
利用本地的QQWry.Dat文件(搜索下载一个QQWry.Dat 文件放到你的wordpress主题根目录下),优点是查询速度非常快,缺点是数据库文件要放在自己的空间内并且要偶尔更新数据库。时间关系废话不多说,下面是使用这个文件的函数,如果是在WordPress里面使用这个功能,把下面的代码写入主题下面的functions.php里面,然后在comments-list的输出即可。仅管理员可见话,则调用代码即可。如果是其他程序引用,输入一个有效的IPv4地址就可以得到一个真实的地址。
以下为引用的内容:<br><br>function convertip($ip) {<br> $dat_path = TEMPLATEPATH."/QQWry.Dat";<br> if(!$fd = @fopen($dat_path, "rb")){<br> return "IP date file not exists or access denied";<br> }<br> $ip = explode(".", $ip);<br> $ipNum = $ip[0] * 16777216 + $ip[1] * 65536 + $ip[2] * 256 + $ip[3];<br> $DataBegin = fread($fd, 4);<br> $DataEnd = fread($fd, 4);<br> $ipbegin = implode("", unpack("L", $DataBegin));<br> if($ipbegin $ipend = implode("", unpack("L", $DataEnd));<br> if($ipend $ipAllNum = ($ipend - $ipbegin) / 7 + 1;<br> $BeginNum = 0;<br> $EndNum = $ipAllNum;<br> while($ip1num>$ipNum $ip2num $Middle= intval(($EndNum + $BeginNum) / 2);<br> fseek($fd, $ipbegin + 7 * $Middle);<br> $ipData1 = fread($fd, 4);<br> if(strlen($ipData1) fclose($fd);<br> return "System Error";<br> }<br> $ip1num = implode("", unpack("L", $ipData1));<br> if($ip1num if($ip1num > $ipNum) {<br> $EndNum = $Middle;<br> continue;<br> }<br> $DataSeek = fread($fd, 3);<br> if(strlen($DataSeek) fclose($fd);<br> return "System Error";<br> }<br> $DataSeek = implode("", unpack("L", $DataSeek.chr(0)));<br> fseek($fd, $DataSeek);<br> $ipData2 = fread($fd, 4);<br> if(strlen($ipData2) fclose($fd);<br> return "System Error";<br> }<br> $ip2num = implode("", unpack("L", $ipData2));<br> if($ip2num if($ip2num if($Middle == $BeginNum) {<br> fclose($fd);<br> return "Unknown";<br> }<br> $BeginNum = $Middle;<br> }<br> }<br> $ipFlag = fread($fd, 1);<br> if($ipFlag == chr(1)) {<br> $ipSeek = fread($fd, 3);<br> if(strlen($ipSeek) fclose($fd);<br> return "System Error";<br> }<br> $ipSeek = implode("", unpack("L", $ipSeek.chr(0)));<br> fseek($fd, $ipSeek);<br> $ipFlag = fread($fd, 1);<br> }<br> if($ipFlag == chr(2)) {<br> $AddrSeek = fread($fd, 3);<br> if(strlen($AddrSeek) fclose($fd);<br> return "System Error";<br> }<br> $ipFlag = fread($fd, 1);<br> if($ipFlag == chr(2)) {<br> $AddrSeek2 = fread($fd, 3);<br> if(strlen($AddrSeek2) fclose($fd);<br> return "System Error";<br> }<br> $AddrSeek2 = implode("", unpack("L", $AddrSeek2.chr(0)));<br> fseek($fd, $AddrSeek2);<br> } else {<br> fseek($fd, -1, SEEK_CUR);<br> }<br> while(($char = fread($fd, 1)) != chr(0))<br> $ipAddr2 .= $char;<br> $AddrSeek = implode("", unpack("L", $AddrSeek.chr(0)));<br> fseek($fd, $AddrSeek);<br> while(($char = fread($fd, 1)) != chr(0))<br> $ipAddr1 .= $char;<br> } else {<br> fseek($fd, -1, SEEK_CUR);<br> while(($char = fread($fd, 1)) != chr(0))<br> $ipAddr1 .= $char;<br><br> $ipFlag = fread($fd, 1);<br> if($ipFlag == chr(2)) {<br> $AddrSeek2 = fread($fd, 3);<br> if(strlen($AddrSeek2) fclose($fd);<br> return "System Error";<br> }<br> $AddrSeek2 = implode("", unpack("L", $AddrSeek2.chr(0)));<br> fseek($fd, $AddrSeek2);<br> } else {<br> fseek($fd, -1, SEEK_CUR);<br> }<br> while(($char = fread($fd, 1)) != chr(0)){<br> $ipAddr2 .= $char;<br> }<br> }<br> fclose($fd);<br> if(preg_match("/http/i", $ipAddr2)) {<br> $ipAddr2 = "";<br> }<br> $ipaddr = "$ipAddr1 $ipAddr2";<br> $ipaddr = preg_replace("/CZ88.Net/is", "", $ipaddr);<br> $ipaddr = preg_replace("/^s*/is", "", $ipaddr);<br> $ipaddr = preg_replace("/s*$/is", "", $ipaddr);<br> if(preg_match("/http/i", $ipaddr) $ipaddr == "") {<br> $ipaddr = "Unknown";<br> }<br> $ipaddr = iconv("gbk", "utf-8//IGNORE", $ipaddr); <br> if( $ipaddr != " " )<br> return $ipaddr;<br> else<br> $ipaddr = "地址未知!火星来客?";<br> return $ipaddr;<br>}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The usage of return in C language is: 1. For functions whose return value type is void, you can use the return statement to end the execution of the function early; 2. For functions whose return value type is not void, the function of the return statement is to end the execution of the function. The result is returned to the caller; 3. End the execution of the function early. Inside the function, we can use the return statement to end the execution of the function early, even if the function does not return a value.

The garbled code in php+fread() is because the encoding of the output page is inconsistent with the encoding of the read file. The solution: 1. Open the corresponding PHP file; 2. Read the file through the fread function; 3. Through "iconv('gbk' , 'utf-8', $data)" method to transcode the read content.

Source code: publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}}#Output The output of the above code can simply conclude: return is executed before finally. Let's take a look at what happens at the bytecode level. The following intercepts part of the bytecode of the case1 method, and compares the source code to annotate the meaning of each instruction in

The fread() function reads data from an open file. The fread() function stops at the end of the file or when it reaches the specified length. Returns the read string on success. Returns FALSE on failure. Syntax fread(file_pointer,length) Parameter file_pointer−The file system pointer resource created using fopen(). Required. length−Maximum number of bytes to read. Required. Return Value If successful, the fread() function returns the read string. On failure, returns FALSE. Suppose we have a file called "one.txt" where

Vue3.2 setup syntax sugar is a compile-time syntax sugar that uses the combined API in a single file component (SFC) to solve the cumbersome setup in Vue3.0. The declared variables, functions, and content introduced by import are exposed through return, so that they can be used in Vue3.0. Problems in use 1. There is no need to return declared variables, functions and content introduced by import during use. You can use syntactic sugar //import the content introduced import{getToday}from'./utils'//variable constmsg='Hello !'//function func

Usage of return in JavaScript requires specific code examples In JavaScript, the return statement is used to specify the value returned from a function. Not only can it be used to end the execution of a function, it can also return a value to the place where the function was called. The return statement has the following common uses: Return a value The return statement can be used to return a value to the place where the function is called. Here is a simple example: functionadd(a,b){

JavaScript functions provide two interfaces to interact with the outside world. The parameters serve as the entrance to receive external information; the return value serves as the outlet to feed back the operation results to the outside world. The following article will take you to understand the JavaScript function return value and briefly analyze the usage of the return statement. I hope it will be helpful to you!

How to use return in JavaScript requires specific code examples. In JavaScript, return is a very important keyword. It is usually used to return a value in a function or end the execution of a function. The return statement is used to return a value to the caller of the function and terminate the execution of the function. The return statement can be used anywhere in a function and can return any JavaScript data type, including numbers, strings, booleans,
