<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>qqwry.dat相关文章列表 | 皇家元林</title>
	<atom:link href="https://hjyl.org/tags/qqwry-dat/feed/" rel="self" type="application/rss+xml" />
	<link>https://hjyl.org</link>
	<description>刘元林的个人博客</description>
	<lastBuildDate>Sun, 25 Aug 2024 11:14:13 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://img.hjyl.org/uploads/2019/10/cropped-about-me-32x32.png</url>
	<title>qqwry.dat相关文章列表 | 皇家元林</title>
	<link>https://hjyl.org</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>基于纯真IP库实现评论者IP归属地</title>
		<link>https://hjyl.org/qqwry-ip-location/</link>
					<comments>https://hjyl.org/qqwry-ip-location/#comments</comments>
		
		<dc:creator><![CDATA[皇家元林]]></dc:creator>
		<pubDate>Tue, 26 Sep 2023 15:07:50 +0000</pubDate>
				<category><![CDATA[元林手札]]></category>
		<category><![CDATA[IP归属地]]></category>
		<category><![CDATA[qqwry.dat]]></category>
		<category><![CDATA[qqwry.ipdb]]></category>
		<category><![CDATA[纯真IP数据库]]></category>
		<guid isPermaLink="false">https://hjyl.org/?p=4655</guid>

					<description><![CDATA[还记得纯真IP——QQWry.dat吗？ 从我开始会上网的时候，外挂版QQ上的小卡片就显示对方的IP地址及其归 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>还记得纯真IP——QQWry.dat吗？</p>



<p>从我开始会上网的时候，外挂版QQ上的小卡片就显示对方的IP地址及其归属地了。</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>来自于<a href="https://hjyl.org/go/aHR0cHM6Ly96aHVhbmxhbi56aGlodS5jb20vcC8zNjA2MjQ5NTI=" rel="nofollow" target="_blank">知乎</a></p>
<cite>QQWry.dat文件是显IP版QQ的数据库文件，用于获取对方IP及位置，纯真IP数据库也采用了这个格式，并沿用至今。<br><br>纯真IP库[<a href="https://hjyl.org/go/aHR0cHM6Ly9jejg4Lm5ldC8=" rel="nofollow" target="_blank">官网</a>]是民间自发收集、提交、聚合而来的数据库，囊括了国内外的大量IP数据，部分记录甚至比商业付费数据库更加准确。它的官网提供了记录提交和纠错的功能，来自全国各地的网友将不同地区的ISP及位置数据上传，管理员在统一整合后每5天更新一次。从2005年到现在的16年间，这个数据库已经聚合了超过五十万条IP记录。<br><br>纯真IP库是完全免费的，它的官网上有在线查询功能，同时也可以下载离线数据库用于低延迟场景，但数据不可用于商业用途。这些记录在稍加分析处理后能达到不错的效果，基本满足定位IP所处城市及ISP信息的需求，不过它目前只支持IPv4地址，在一些应用场景下稍显不足。</cite></blockquote>



<p>话不多说，言归正传，此PHP解析纯真IP数据库的源代码来自于<a href="https://hjyl.org/go/aHR0cHM6Ly9naXRodWIuY29tL2l0YmR3L2lwLWRhdGFiYXNl" rel="nofollow" target="_blank">itbdw/ip-database</a>，有很长一段时间没更新了，最新版本为3.X，支持ipv4+ipv6，如需要2.x稳定版请访问 <a href="https://hjyl.org/go/aHR0cHM6Ly9naXRodWIuY29tL2l0YmR3L2lwLWRhdGFiYXNlL3RyZWUvMi54" rel="nofollow" target="_blank">https://github.com/itbdw/ip-database/tree/2.x</a>。</p>



<p>下载好文件IpLocation.php、QQwry.php、IpV6wry.php、StringParser.php、IpParserInterface.php，放在一个文件夹里（或者直接用<a href="https://alist.hjyl.org/d/%E7%BE%A4%E6%99%96%E7%B3%BB%E5%88%97/qqwry.zip">我整理好的文件</a>），上传到主题目录里。</p>



<p>把以下代码甩进functions.php即可：</p>



<pre class="wp-block-code"><code lang="php" class="language-php line-numbers">//解析纯真IP获取评论者ip归属地 开始
//Code By HJYL.ORG
require get_template_directory() . '/qqwry/IpParserInterface.php';
require get_template_directory() . '/qqwry/IpLocation.php';
require get_template_directory() . '/qqwry/QQwry.php';
require get_template_directory() . '/qqwry/IpV6wry.php';
require get_template_directory() . '/qqwry/StringParser.php';
use itbdw\Ip\IpLocation;
function getCity($ip){
    $qqwry_filepath = get_template_directory() .'/qqwry/qqwry.dat';
    $getLocation = json_encode(IpLocation::getLocation($ip, $qqwry_filepath), JSON_UNESCAPED_UNICODE) . "\n";
    $IPinfo = json_decode($getLocation, true);
    $country = $IPinfo['country'];
    $province = $IPinfo['province'];
/***************************************
    //优先显示城市，其次省，最后国家
    $city = $IPinfo['city'];
	if($ip == '127.0.0.1'){
		$data = '小黑屋';
	}elseif($city != ""){
        $data = $city;
    }elseif($province !== "" &amp;&amp; $city == ""){
        $data = $province;
    }else{
    	$data = $country;
    }
**************************************/
	if($ip == '127.0.0.1'){
		$data = '小黑屋';
    }elseif($province !== ""){
        $data = $province;
    }else{
    	$data = $country;
    }
    
    return $data;
}
//解析纯真IP获取评论者ip归属地 结束</code></pre>



<p>调用方法：</p>



<pre class="wp-block-code"><code lang="php" class="language-php line-numbers">echo getCity(get_comment_author_IP());</code></pre>



<p>它支持composer写入内存，再读取：</p>



<pre class="wp-block-code"><code lang="php" class="language-php line-numbers">composer require 'itbdw/ip-database'   #注意^3.0只支持PHP5.4~7.0</code></pre>



<p>到此结束！</p>



<p>画外音：折腾了一圈才发现，纯真数据库qqwry.dat也有 IPIP.net 格式版——qqwry.ipdb，来自于<a href="https://hjyl.org/go/aHR0cHM6Ly9naXRodWIuY29tL21ldG93b2xmL3Fxd3J5LmlwZGIvdHJlZS9tYXN0ZXI=" rel="nofollow" target="_blank">metowolf /qqwry.ipdb</a>，目前最新纯真IP免费版数据库日期为2023.09.20。也就是说我之前用的大发分享的WordPress插件Easy Location IP数据库也可以更新到纯真IP最新版数据库了，不想麻烦的，也可以用这个。</p>



<p>—— 完 ——</p>
<div id="content-copyright"><span style="font-weight:bold;text-shadow:0 1px 0 #ddd;font-size: 13px;">版权声明: </span><span style="font-size: 13px;">本文采用 <a href="https://hjyl.org/go/aHR0cHM6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL2xpY2Vuc2VzL2J5LW5jLXNhLzMuMC8=" rel="nofollow" target="_blank">BY-NC-SA</a> 协议进行授权，如无注明均为原创，转载请注明转自 <a href="https://hjyl.org">皇家元林</a><br>本文链接: <a rel="bookmark" title="基于纯真IP库实现评论者IP归属地" href="https://hjyl.org/qqwry-ip-location/">基于纯真IP库实现评论者IP归属地</a></span></div>]]></content:encoded>
					
					<wfw:commentRss>https://hjyl.org/qqwry-ip-location/feed/</wfw:commentRss>
			<slash:comments>14</slash:comments>
		
		
			</item>
	</channel>
</rss>
