还是记录好ip,做好来访次数统计。等来访超出一定的阀值,就开始超时请求链接和转跳。
不让更多的流量对外输出!
<?php $ip = getIP(); $data = file_get_contents('./ip20181128.txt'); if (empty($data)) { $data = array(); } else { $data = json_decode($data, true); } //一小时内 $time = time() + 3600 * 1; //10次请求 $number = 10; //默认访问 $visit = true; if (count($data) > 0) { if (!empty($data[$ip])) { $click = $data[$ip]['click'] + 1; $out_time = isset($data[$ip]['out_time']) ? $data[$ip]['out_time'] : ''; if ($click > $number && $out_time > time()) { $visit = false; } if ($out_time > time() || !empty($out_time)) { $data[$ip]['click'] = $click; } else { $data[$ip] = array('click' => 1, 'time' => time(), 'out_time' => $time); } } else { $data[$ip] = array('click' => 1, 'time' => time(), 'out_time' => $time); } } else { $data[$ip] = array('click' => 1, 'time' => time()); } file_put_contents('./ip20181128.txt', json_encode($data)); if (!$visit) { sleep(600); header("location:https://127.0.0.1:3306/aasdas"); exit; } function getIP() { if (getenv('HTTP_CLIENT_IP')) { $ip = getenv('HTTP_CLIENT_IP'); } elseif (getenv('HTTP_X_FORWARDED_FOR')) { $ip = getenv('HTTP_X_FORWARDED_FOR'); } elseif (getenv('HTTP_X_FORWARDED')) { $ip = getenv('HTTP_X_FORWARDED'); } elseif (getenv('HTTP_FORWARDED_FOR')) { $ip = getenv('HTTP_FORWARDED_FOR'); } elseif (getenv('HTTP_FORWARDED')) { $ip = getenv('HTTP_FORWARDED'); } else { $ip = $_SERVER['REMOTE_ADDR']; } return $ip; }
关键字词: