Typecho在评论增加IP归属地址展示
AI摘要 文章介绍了如何在Typecho评论中增加IP归属地址展示。首先,需要打开主题文件`/functions.php`并添加两段代码,第一段代码用于获取IP信息并显示省份,第二段代码用于同时获取省份和城市信息。最后,如果未查询到IP,则显示“IP归属:未知”。
引用:https://developer.aliyun.com/article/1584014
参考内容
发现显示出来格式的是:中国 山东省 青岛市 联通
修改完善
然而实际使用多数只保留 省份+市区 即可
操作方法
- 打开主题文件:
/functions.php - 增加以下代码
/**
* 留言IP
*
* @param int $ip ip地址
*/
function getIPInfo($ip) {
$url = "http://ip.plyz.net/ip.ashx?ip={$ip}";
$data = curlRequest($url, 'GET');
$ipParts = explode("|", $data);
// 默认显示PLYZ.NET(获取失败时)
$result = 'PLYZ.NET';
// 检查数据格式是否正确
if (isset($ipParts[1]) && !empty(trim($ipParts[1]))) {
// 拆分地域信息(如:中国 河南省 郑州市 移动)
$locationParts = explode(" ", $ipParts[1]);
// 提取省份部分(索引1)并处理
if (isset($locationParts[1]) && !empty(trim($locationParts[1]))) {
// 去除省份名后的"省"字(如"河南省"→"河南")
$province = rtrim($locationParts[1], '省');
// 确保处理后不为空
if (!empty(trim($province))) {
$result = $province;
}
}
}
echo $result;
}
/**
* 多种请求方法封装
*
* @param string $url 请求地址
* @param string $method 请求方式
* @param array $header 请求头
* @param array $data 请求体
*
* @return mixed
*/
function curlRequest($url, $method = 'POST', $header = ["Content-type:application/json;charset=utf-8", "Accept:application/json"], $data = [])
{
$method = strtoupper($method);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // 修复SSL验证参数
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
if (!empty($data)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 增加超时设置,避免请求阻塞
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$res = curl_exec($ch);
curl_close($ch);
return $res;
}- 调用方式不变,根据自己喜好
<?php getIPInfo($comments->ip) ?>
(上面显示格式为:ID归属:北京)
以下代码显示格式为:ID归属:山东青岛
/**
* 留言IP
*
* @param int $ip ip地址
*/
function getIPInfo($ip) {
$url = "http://ip.plyz.net/ip.ashx?ip={$ip}";
$data = curlRequest($url, 'GET');
$ipParts = explode("|", $data);
// 默认显示PLYZ.NET(获取失败时)
$result = 'PLYZ.NET';
// 检查数据格式是否正确
if (isset($ipParts[1]) && !empty(trim($ipParts[1]))) {
// 拆分地域信息(如:中国 河南省 新乡市 移动)
$locationParts = explode(" ", $ipParts[1]);
// 同时提取省份和城市并处理
if (isset($locationParts[1], $locationParts[2]) && !empty(trim($locationParts[1])) && !empty(trim($locationParts[2]))) {
// 去除省份后缀“省”、城市后缀“市”
$province = rtrim(trim($locationParts[1]), '省');
$city = rtrim(trim($locationParts[2]), '市');
// 确保处理后不为空
if (!empty($province) && !empty($city)) {
$result = $province . $city; // 拼接为“河南新乡”
}
}
}
echo $result;
}
/**
* 多种请求方法封装
*
* @param string $url 请求地址
* @param string $method 请求方式
* @param array $header 请求头
* @param array $data 请求体
*
* @return mixed
*/
function curlRequest($url, $method = 'POST', $header = ["Content-type:application/json;charset=utf-8", "Accept:application/json"], $data = [])
{
$method = strtoupper($method);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // 修复SSL验证参数
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
if (!empty($data)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 增加超时设置,避免请求阻塞
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$res = curl_exec($ch);
curl_close($ch);
return $res;
}如果未查询到IP,则强制显示为:IP归属:未知
212