Preface#
CDN can accelerate websites and protect the source IP of the website. However, when Typecho enables CDN, the IP addresses of users in the backend comment area are all displayed as the CDN's IP. So how can we obtain the real IP addresses of users?
Tutorial#
First, open [this][1]
Get your own IP address, for example, 2001:470:c:573::2333
Create a new PHP file and enter the following content
<?php
print_r($_SERVER);
?>
Then access it through a browser and get the following content
*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
)*
Press Ctrl+F and search for your IP address 2001:470:c:573::2333
The corresponding item is
HTTP_CF_CONNECTING_IP
Get it using PHP
<?php
$ipaddr=$_SERVER['HTTP_CF_CONNECTING_IP'];
echo $ipaddr
?>
Then you can get the real user IP address
So how to apply it to Typecho
Find the config.inc.php in the root directory of the website
Add the following content at the end of the file, leaving a few blank lines
define('__TYPECHO_IP_SOURCE__', 'CF_CONNECTING_IP'); //Remove HTTP_
After saving, open the website
Leave a comment in the comment area
You will find that it is the real IP of the user, not the CDN's
This operation will not change the IP of existing users in the comment area, and the CDN IP displayed for existing users in the database will not change:::
This article is synchronized and updated to xLog by Mix Space
The original link is https://blog.nekorua.com/posts/maintain/14.html