php页面api接口设置访问频率简单方法
session_start();if (isset($_SESSION['LAST_CALL'])) {
$last = strtotime($_SESSION['LAST_CALL']);
$curr = strtotime(date("Y-m-d h:i:s"));
$sec =abs($last - $curr);
if ($sec <= 1) {
$data = '访问频率超限';// rate limit
header('Content-Type: application/json');
die (json_encode($data,JSON_UNESCAPED_UNICODE));//另外如果需要返回中文提示,json_encode需要加个选项,否则web页面显示转译的unicode字符
}
}
$_SESSION['LAST_CALL'] = date("Y-m-d h:i:s");
// normal usage
$data = "接口返回正常数据";
header('Content-Type: application/json');
die(json_encode($data));
页:
[1]