ThinkPHP5获取客户端真实IP地址方法 - 解决$_SERVER[X-Real-IP]为空问题
"ThinkPHP5获取客户端真实IP地址方法 - 解决$_SERVER[X-Real-IP]为空问题"\n\nThinkPHP5中获取客户端真实IP地址的最佳方法是使用Request::ip()方法,该方法会自动处理代理服务器情况。如果您在Nginx中正确设置了proxy_set_header X-Real-IP $http_x_real_ip,并且在Nginx的access.log中能够正确打印http_x_real_ip,但$_SERVER['X-Real-IP']仍然为空,您可以使用Request::header('X-Real-IP')获取X-Real-IP头部的值。\n\n以下代码演示如何使用Request::ip()和Request::header('X-Real-IP')获取客户端真实IP地址:\n\nphp\nuse think\facade\Request;\n\n// 使用Request::ip()获取客户端真实IP地址\n$ip = Request::ip();\n\n// 使用Request::header('X-Real-IP')获取X-Real-IP头部的值\n$realIP = Request::header('X-Real-IP');\n\n\n希望以上信息对您有所帮助。
原文地址: https://www.cveoy.top/t/topic/pMcC 著作权归作者所有。请勿转载和采集!