Example of creating abnormal login IP detection function using PHP

墨辰丷
Release: 2023-03-28 15:00:01
Original
1691 people have browsed it

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 = '登录IP:".$ip.""; }else{ //不同则记数这个ip地址数量 $count = M('log')->where("name='{$name}' AND ip='{$ip}'")->count(); //如果超过一定数量则是正常ip否则为异常返回字符串 if($count > 10){ $str = '登录IP:".$ip.""; }else{ $str = '异常IP:".$ip.""; } } return $str; }
Copy after login

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; }
Copy after login

The above is the entire content of this article, I hope it will be useful to Everyone’s learning helps.


Related recommendations:

phpHow to quickly remove duplicates from array elements

PHP method of printing binary trees in zigzag order

##phpDetailed 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!