前言#
CDN 可以起到加速网站和保护源站 IP 的作用,但是 Typecho 启用 CDN 后,后台评论区用户的 IP 均显示为 CDN 的 IP,那么怎样才能获取用户的真实 IP 地址呢?
教程#
首先打开 [这个][1]
获取自己的 IP 地址,比如说 2001:470:c:573::2333
新建一个 PHP 文件,输入以下内容
<?php
print_r($_SERVER);
?>
然后通过浏览器访问,得到以下内容
*Array
(
[USER] => www
[HOME] => /home/www
[HTTP_CDN_LOOP] => cloudflare
[HTTP_CF_CONNECTING_IP] => 2001:470:c:573::2333
[HTTP_COOKIE] => __cfduid=1a1d45v141f9n19s810b
[HTTP_ACCEPT_LANGUAGE] => zh-CN,zh;q=0.9
[HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/awebp,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
[HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36
[HTTP_UPGRADE_INSECURE_REQUESTS] => 1
[HTTP_CF_VISITOR] => {"scheme":"https"}
[HTTP_X_FORWARDED_PROTO] => https
[HTTP_CF_RAY] => 5656aff39c0ceb95-LAX
[HTTP_X_FORWARDED_FOR] => 2001:470:c:573::2333
[HTTP_CF_IPCOUNTRY] => US
[HTTP_ACCEPT_ENCODING] => gzip
[HTTP_CONNECTION] => Keep-Alive
[HTTP_HOST] => blog.xcnya.cn
[PATH_INFO] => 114514
[REDIRECT_STATUS] => 200
[SERVER_NAME] => blog.xcnya.cn
[SERVER_PORT] => 443
[SERVER_ADDR] => 2606:4700:3036::2333:2333
[REMOTE_PORT] => 11451
[REMOTE_ADDR] => 2606:4700:3036::2333:2333
[SERVER_SOFTWARE] => cloudflare
[GATEWAY_INTERFACE] => CGI/1.1
[REQUEST_SCHEME] => https
[SERVER_PROTOCOL] => HTTP/1.1
[DOCUMENT_ROOT] => /www/wwwroot/blog.xcnya.cn
[DOCUMENT_URI] => /114514.php
[REQUEST_URI] => /114514.php
[SCRIPT_NAME] => /114514.php
[CONTENT_LENGTH] =>
[CONTENT_TYPE] =>
[REQUEST_METHOD] => GET
[QUERY_STRING] =>
[SCRIPT_FILENAME] => /www/wwwroot/blog.xcnya.cn
[FCGI_ROLE] => RESPONDER
[PHP_SELF] => /114514.php
[REQUEST_TIME_FLOAT] => 1656230855.1145
[REQUEST_TIME] => 1656230855
)*
按 Ctrl+F,在里面搜索你的 IP 地址 2001:470:c:573::2333
得到对应的项为
HTTP_CF_CONNECTING_IP
用 PHP 获取
<?php
$ipaddr=$_SERVER['HTTP_CF_CONNECTING_IP'];
echo $ipaddr
?>
然后就能得到真实的用户 IP 地址
那么如何应用到 Typecho 呢
在网站根目录的找到 config.inc.php
在该文件的末尾空几行添加以下内容
define('__TYPECHO_IP_SOURCE__', 'CF_CONNECTING_IP'); //去掉HTTP_
保存后打开网站
自己在评论区里随便发条评论
发现已经是用户真实的 IP 了,而不是 CDN 的了
此操作不会改变现有评论区用户的 IP,操作后数据库中原有用户显示的 CDN IP 不会变化:::
此文由 Mix Space 同步更新至 xLog
原始链接为 https://blog.nekorua.com/posts/maintain/14.html