Home > Article > Backend Development > How to verify IP legitimacy with PHP
This article mainly introduces how to verify the legality of IP with PHP, which has certain reference value. Now I share it with you. Friends in need can refer to it
function get_ip(){ //判断服务器是否允许$_SERVER if(isset($_SERVER)){ if(isset($_SERVER[HTTP_X_FORWARDED_FOR])){ $realip = $_SERVER[HTTP_X_FORWARDED_FOR]; }elseif(isset($_SERVER[HTTP_CLIENT_IP])) { $realip = $_SERVER[HTTP_CLIENT_IP]; }else{ $realip = $_SERVER[REMOTE_ADDR]; } }else{ //不允许就使用getenv获取 if(getenv("HTTP_X_FORWARDED_FOR")){ $realip = getenv( "HTTP_X_FORWARDED_FOR"); }elseif(getenv("HTTP_CLIENT_IP")) { $realip = getenv("HTTP_CLIENT_IP"); }else{ $realip = getenv("REMOTE_ADDR"); } } return $realip; }
The above examples can be used in the interface! Add IP verification mechanism!
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
How to improve web page loading speed in php
5 ways to generate a unique order number in php
The above is the detailed content of How to verify IP legitimacy with PHP. For more information, please follow other related articles on the PHP Chinese website!