php接口限流主要是防止高併發造成服務器扛不住的情況下,需要限制數據的獲取,簡單實現就是結合redis實現。
<?php
/**
* api 接口限流
*
*/
class api
{
public function get_client_ip($type = 0) {
$type = $type ? 1 : 0;
static $ip = NULL;
if ($ip !== NULL) return $ip[$type];
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
$pos = array_search('unknown',$arr);
if(false !== $pos) unset($arr[$pos]);
$ip = trim($arr[0]);
}elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
}elseif (isset($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR'];
}
// IP地址合法驗證
$long = ip2long($ip);
$ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
return $ip[$type];
}
public function test()
{
//接口時間限流,這種方式可以防止鑽時間漏洞無限的訪問接口 比如在59秒的時候訪問,就鑽了空子
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$ip = $this->get_client_ip(true);
$len = $redis->lLen($ip);
if($len === 0)
{
$redis->lPush($ip,time());
echo "訪問1次<br>";
$redis->expire($ip,60);
}else{
//判斷有沒有超過1分鐘
$max_time = $redis->lRange($ip,0,0);
//判斷最後一次訪問的時間比對是否超過了1分鐘
if((time()- $max_time[0]) < 60){
if($len> 10){
echo '訪問超過了限制';
}else{
$redis->lPush($ip,time());
echo "訪問{$len}次<br>";
}
}
}
}
}
(new api())->test();