php配合Redis防網站cc攻擊或採集
阿新 • • 發佈:2020-10-21
$redis = new Redis(); $redis->connect('127.0.0.1', 6379); //獲取客戶端真實ip地址 來源:這家軟體站 www.zjkweiqi.cn function get_real_ip(){ static $realip; if(isset($_SERVER)){ if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){ $realip=$_SERVER['HTTP_X_FORWARDED_FOR']; }else if(isset($_SERVER['HTTP_CLIENT_IP'])){ $realip=$_SERVER['HTTP_CLIENT_IP']; }else{ $realip=$_SERVER['REMOTE_ADDR']; } }else{ if(getenv('HTTP_X_FORWARDED_FOR')){ $realip=getenv('HTTP_X_FORWARDED_FOR'); }else if(getenv('HTTP_CLIENT_IP')){$realip=getenv('HTTP_CLIENT_IP'); }else{ $realip=getenv('REMOTE_ADDR'); } } return $realip; } //這個key記錄該ip的訪問次數 也可改成使用者id $key = get_client_ip(); //該Key記錄訪問的次數,目前是以IP為例,也可以把使用者id作為key,如userid_123456 //限制次數為3次。 $limit = 3; $check = $redis->exists($key); if($check){ $redis->incr($key); $count = $redis->get($key); if($count > 3){ exit('已經超出了限制次數'); } }else{ $redis->incr($key); //限制時間為60秒 $redis->expire($key,60); } $count = $redis->get($key);