Commenters add IP address attribution information

Original link: https://yf-ch.com/archives/1951.html

Recently, Baidu, Weibo, Douyin, Zhihu, and Xiaohongshu have all launched the comment IP territorial function, which is displayed in provinces/regions in China and in countries abroad, and users cannot turn off this function.

Weibo has always been committed to maintaining a healthy and orderly discussion atmosphere and ensuring users’ rights to obtain real and effective information in the first place. In order to reduce bad behaviors such as impersonating the parties involved in hot events, malicious rumors, and rubbing traffic, and to ensure the authenticity and transparency of the disseminated content, the website launched the user’s “IP territorial” function in March this year.

In order to maintain a real and orderly discussion atmosphere, Douyin Security Center official WeChat messages reduce bad behaviors such as impersonating parties to hot events, malicious rumors, and traffic rubbing. Douyin plans to display the account IP location on the personal homepage and other locations. The relevant functions will be tested at the end of April, and will be gradually launched in other scenarios according to the test situation. The account IP location displayed on the platform is the network location at the time of the last post or comment. It is displayed in the province (region, city) domestically, and displayed in the country (region) overseas. The account IP location is subject to the information provided by the operator, and related display is not supported. Turn it on or off manually.

I have time to toss these days, and decided to keep up with this trend. With this function, my blog is built by Typecho. Currently, there are two ways to test it: one is to call the APIs of websites such as Baidu, Pacific, and Taobao. The problem is It affects the loading speed of the website; the second is to use the local database, which is much faster.
So the second one I use utilizes the naive IP database.

The specific effect is as follows

The specific operations are as follows

the first method

Here we use the Pacific Network IP address to query the web interface parameters. The first one is ip, which can be obtained from the comment form. The second one is level, which is used to set the output precision, =1/=2/=3 respectively Represents only output province name/output province and city name/output province and city name The third is domId, which is used to specify the node ID

Then I started to first find the code for the comment display of the theme I am using now. This location is different according to the theme. Mine is in the comments.php file in the theme folder and then adds a tag where I need it, this tag The id needs to be the id of the comment. You can add any prefix to distinguish it. Finally, add the js script after the tag. Note in the url parameter of the script: the level is customized as needed, the domId is the same as the id of the above tag, and the ip is The ip of this comment.

 <span class="tg">来自<span id="ip-<?php $comments->theId();?>"></span></span> <script src="https://whois.pconline.com.cn/jsDom.jsp?level=1&domId=ip-<?php $comments->theId(); ?>&ip=<?php echo $comments->ip; ?>"></script>

Add the no-referrer tag to the head of the blog theme

 <meta name="referrer" content="no-referrer"/>

The effect is as shown in the figure

The second method

Put the following code into the theme’s functions.php file, which can be inserted at the very end.

code
 /** 获取评论者地址*/ function convertip($ip){ $ip1num = 0; $ip2num = 0; $ipAddr1 =""; $ipAddr2 =""; $dat_path = './qqwry.dat'; if(!preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/", $ip)) { return 'IP 数据库路径不对'; } if(!$fd = @fopen($dat_path, 'rb')){ return 'IP 数据库路径不正确'; } $ip = explode('.', $ip); $ipNum = $ip[0] * 16777216 + $ip[1] * 65536 + $ip[2] * 256 + $ip[3]; $DataBegin = fread($fd, 4); $DataEnd = fread($fd, 4); $ipbegin = implode('', unpack('L', $DataBegin)); if($ipbegin < 0) $ipbegin += pow(2, 32); $ipend = implode('', unpack('L', $DataEnd)); if($ipend < 0) $ipend += pow(2, 32); $ipAllNum = ($ipend - $ipbegin) / 7 + 1; $BeginNum = 0; $EndNum = $ipAllNum; while($ip1num>$ipNum || $ip2num<$ipNum) { $Middle= intval(($EndNum + $BeginNum) / 2); fseek($fd, $ipbegin + 7 * $Middle); $ipData1 = fread($fd, 4); if(strlen($ipData1) < 4) { fclose($fd); return 'System Error'; } $ip1num = implode('', unpack('L', $ipData1)); if($ip1num < 0) $ip1num += pow(2, 32); if($ip1num > $ipNum) { $EndNum = $Middle; continue; } $DataSeek = fread($fd, 3); if(strlen($DataSeek) < 3) { fclose($fd); return 'System Error'; } $DataSeek = implode('', unpack('L', $DataSeek.chr(0))); fseek($fd, $DataSeek); $ipData2 = fread($fd, 4); if(strlen($ipData2) < 4) { fclose($fd); return 'System Error'; } $ip2num = implode('', unpack('L', $ipData2)); if($ip2num < 0) $ip2num += pow(2, 32); if($ip2num < $ipNum) { if($Middle == $BeginNum) { fclose($fd); return 'Unknown'; } $BeginNum = $Middle; } } $ipFlag = fread($fd, 1); if($ipFlag == chr(1)) { $ipSeek = fread($fd, 3); if(strlen($ipSeek) < 3) { fclose($fd); return 'System Error'; } $ipSeek = implode('', unpack('L', $ipSeek.chr(0))); fseek($fd, $ipSeek); $ipFlag = fread($fd, 1); } if($ipFlag == chr(2)) { $AddrSeek = fread($fd, 3); if(strlen($AddrSeek) < 3) { fclose($fd); return 'System Error'; } $ipFlag = fread($fd, 1); if($ipFlag == chr(2)) { $AddrSeek2 = fread($fd, 3); if(strlen($AddrSeek2) < 3) { fclose($fd); return 'System Error'; } $AddrSeek2 = implode('', unpack('L', $AddrSeek2.chr(0))); fseek($fd, $AddrSeek2); } else { fseek($fd, -1, SEEK_CUR); } while(($char = fread($fd, 1)) != chr(0)) $ipAddr2 .= $char; $AddrSeek = implode('', unpack('L', $AddrSeek.chr(0))); fseek($fd, $AddrSeek); while(($char = fread($fd, 1)) != chr(0)) $ipAddr1 .= $char; } else { fseek($fd, -1, SEEK_CUR); while(($char = fread($fd, 1)) != chr(0)) $ipAddr1 .= $char; $ipFlag = fread($fd, 1); if($ipFlag == chr(2)) { $AddrSeek2 = fread($fd, 3); if(strlen($AddrSeek2) < 3) { fclose($fd); return 'System Error'; } $AddrSeek2 = implode('', unpack('L', $AddrSeek2.chr(0))); fseek($fd, $AddrSeek2); } else { fseek($fd, -1, SEEK_CUR); } while(($char = fread($fd, 1)) != chr(0)){ $ipAddr2 .= $char; } } fclose($fd); if(preg_match('/http/i', $ipAddr2)) { $ipAddr2 = ''; } $ipaddr = "$ipAddr1 $ipAddr2"; $ipaddr = preg_replace('/CZ88.NET/is', '', $ipaddr); $ipaddr = preg_replace('/^s*/is', '', $ipaddr); $ipaddr = preg_replace('/s*$/is', '', $ipaddr); if(preg_match('/http/i', $ipaddr) || $ipaddr == '') { $ipaddr = '可能来自火星'; } $ipaddr = iconv('gbk', 'utf-8//IGNORE', $ipaddr); return $ipaddr; }

Download QQWry.Dat and put it in the root directory of the website, pay attention to the root directory of the website!

Baidu network disk extraction code: ju6v Tencent Weiyun password: knruma

Open the theme comments template comments.php file and add the following code to the location you want to display:

 <?php echo convertip($comments->ip); ?>

After Typecho configures the CDN, it may be affected to obtain the real IP address of the visitor. Please add the following code to config.inc.php in the root directory of the Typecho site. Some virtual hosts will have their own CDN, so even if the CDN is not configured, as long as it cannot be displayed normally, you can try to insert the following code.

 //绕过CDN 代理IP获取客户真实IP地址if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $list = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']); $_SERVER['REMOTE_ADDR'] = $list[0]; }

The effect is as shown in the figure

This article is reproduced from: https://yf-ch.com/archives/1951.html
This site is for inclusion only, and the copyright belongs to the original author.