This article mainly introduces the example of creating abnormal login IP detection function in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.
Use function query database traversal implementation
/** * 不在常用ip地址登录返回描红信息 * @param string $ip ip地址 * @param string $name 用户名 * @return string */ function errorIp($ip,$name){ $nowip = get_client_ip(); //判断ip和当前ip是否相同,不同则查询数据库对比 if($ip == $nowip ){ //相同直接返回字符串 $str = '<font color="blue"'.">登录IP:".$ip."</font>"; }else{ //不同则记数这个ip地址数量 $count = M('log')->where("name='{$name}' AND ip='{$ip}'")->count(); //如果超过一定数量则是正常ip否则为异常返回字符串 if($count > 10){ $str = '<font color="blue"'.">登录IP:".$ip."</font>"; }else{ $str = '<font color="red"'.">异常IP:".$ip."</font>"; } } return $str; }
Note: Suitable for all frameworks, get_client_ip() Is the ip acquisition function.
get_client_ip function fragment:
function get_client_ip() { if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) { $ip = getenv('HTTP_CLIENT_IP'); } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) { $ip = getenv('HTTP_X_FORWARDED_FOR'); } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) { $ip = getenv('REMOTE_ADDR'); } else{ $ip = $_SERVER['REMOTE_ADDR']; } return $ip; }
The above is the entire content of this article, I hope it will be useful to Everyone’s learning helps.
Related recommendations:
php How to quickly remove duplicates from array elements
PHP method of printing binary trees in zigzag order
##php Detailed explanation of how to set up a session that strictly controls the expiration time
The above is the detailed content of Example of creating abnormal login IP detection function using PHP. For more information, please follow other related articles on the PHP Chinese website!