Browse Source

add frequence limit function

master
filesite 7 months ago
parent
commit
e1f3125b50
  1. 15
      controller/Controller.php
  2. 61
      themes/tajian/controller/FrontapiController.php
  3. 4
      views/layout/error.php
  4. 2
      www/js/tajian.js

15
controller/Controller.php

@ -162,6 +162,21 @@ Class Controller { @@ -162,6 +162,21 @@ Class Controller {
}
}
//error log
protected function logError($error_message) {
if (!empty(FSC::$app['config']['debug'])) {
$thisUrl = FSC::$app['requestUrl'];
$logTime = date('Y-m-d H:i:s');
$logDir = __DIR__ . '/../runtime/logs/';
$logFilename = 'error.log';
$logOk = @error_log("{$logTime}\t{$thisUrl}\tERROR: {$error_message}\n", 3, "{$logDir}{$logFilename}");
if (!$logOk) { //try to mkdir
@mkdir($logDir, 0700, true);
@error_log("{$logTime}\t{$thisUrl}\ttERROR: {$error_message} ms\n", 3, "{$logDir}{$logFilename}");
}
}
}
//get user real ip
protected function getUserIp() {
$ip = false;

61
themes/tajian/controller/FrontapiController.php

@ -372,4 +372,65 @@ eof; @@ -372,4 +372,65 @@ eof;
exit;
}
//请求频率限制
/**
* key: 检查频率限制的唯一标识
* max: 最大次数
* time: 检查时间,单位:秒
*/
protected function requestLimit($key, $max, $time) {
$isLimited = false;
try {
session_start();
$current_time = microtime(true)*1000;
$field = md5("requestLimit_by_{$key}");
$field_update_time = "{$field}_updated";
if (!empty($_SESSION[$field]) && !empty($_SESSION[$field_update_time]) && $current_time - $_SESSION[$field_update_time] <= $time*1000) {
$_SESSION[$field] ++;
}else {
$_SESSION[$field] = 1;
$_SESSION[$field_update_time] = $current_time;
}
if ($_SESSION[$field] > $max) {
$isLimited = true;
}
}catch(Exception $e) {
$this->logError("Request limit by session failed: " . $e->getMessage());
}
return $isLimited;
}
//获取短信验证码
public function actionSendsmscode() {
echo "Building...";
exit;
}
//新用户注册
public function actionCreateuser() {
$ip = $this->getUserIp();
$check_time = 120; //2 分钟内
$max_time_in_minutes = 5; //最多 5 次
$isUserGotRequestLimit = $this->requestLimit($ip, $max_time_in_minutes, $check_time);
if ($isUserGotRequestLimit) {
$this->logError("Request limit got, ip: {$ip}");
throw new Exception('Oops,操作太快了,请喝杯咖啡休息会吧...');
}
echo "Building...";
exit;
}
//用户登录
public function actionLoginuser() {
echo "Building...";
exit;
}
}

4
views/layout/error.php

@ -5,9 +5,9 @@ @@ -5,9 +5,9 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
<style>
body{max-width:768px;margin:0 auto}
body{max-width:768px;margin:0 auto;padding:10px}
h1{color:#FF0000}
.error{padding:4px;color:yellow;background-color:gray;min-height:40px}
.error{padding:4px 10px;color:yellow;background-color:gray;min-height:40px}
</style>
</head>
<body>

2
www/js/tajian.js

@ -203,7 +203,7 @@ if ($('.bt_kf_JS').get(0)) { @@ -203,7 +203,7 @@ if ($('.bt_kf_JS').get(0)) {
var win_width = $(window).width();
if (win_width > 768 && $('.tajian_index').get(0)) {
$('.bt_kf_JS').click();
$('.bt_kf_JS').click().addClass('hide');
}
}

Loading…
Cancel
Save