Authored by xiaowei

防撞库修改

... ... @@ -143,20 +143,15 @@ class PassportModel
* $expiry 缓存时间
*/
public static function increment($key, $timeKey, $offset = 1, $initValue = 0, $expire = 1800)
public static function increment($key, $offset = 1, $initValue = 0, $expire = 1800)
{
//有效期之外清除key
if (!Cache::get($timeKey) && Cache::get($key)) {
Cache::delete($key);
}
//初始化计时
if (!Cache::get($timeKey) && !Cache::get($key)) {
Cache::set($timeKey, TRUE, $expire);
Cache::set($key, $initValue);
if (!Cache::get($key)) {
Cache::set($key, $initValue, $expire);
}
//未过期则递增
//递增,过期则重新计算
$cacheValue = intval(Cache::get($key)) + $offset;
Cache::set($key, $cacheValue);
Cache::set($key, $cacheValue, $expire);
}
}
... ...
... ... @@ -108,24 +108,14 @@ class LoginController extends WebAction
*/
$ip = Helpers::getClientIp();
$ipKey = md5('ip_signin_' . $ip);
$ipTimeKey = md5('ip_signin_time' . $ip);
$accountKey = md5('account_signin_' . $account);
$accountTimeKey = md5('account_signin_time' . $account);
//cache初始化,非有效时间内清除次数,有效时间内叠加cache计数
if (!Cache::get($accountTimeKey) && Cache::get($accountKey)) {
Cache::delete($accountKey);
if (!Cache::get($accountKey)) {
Cache::set($accountKey, 0, 1800);
}
if (!Cache::get($accountTimeKey) && !Cache::get($accountKey)) {
Cache::set($accountTimeKey, true, 1800);
Cache::set($accountKey, 0);
}
if (!Cache::get($ipTimeKey) && Cache::get($ipKey)) {
Cache::delete($ipKey);
}
if (!Cache::get($ipTimeKey) && !Cache::get($ipKey)) {
Cache::set($ipTimeKey, true, 3600);
Cache::set($ipKey, 0);
if (!Cache::get($ipKey)) {
Cache::set($ipKey, 0, 3600);
}
//调用接口前校验次数
$accountTimes = Cache::get($accountKey);
$ipTimes = Cache::get($ipKey);
if ($accountTimes >= 10) {
... ... @@ -138,8 +128,19 @@ class LoginController extends WebAction
}
$data = LoginData::signin($area, $account, $password, $shoppingKey);
if (!isset($data['code']) || $data['code'] != 200 || !isset($data['data']['uid'])) {
Cache::set($accountKey, intval(Cache::get($accountKey)) + 1);
Cache::set($ipKey, intval(Cache::get($ipKey)) + 1);
Cache::set($accountKey, intval(Cache::get($accountKey)) + 1, 1800);
Cache::set($ipKey, intval(Cache::get($ipKey)) + 1, 3600);
//再次校验
$accountTimes = Cache::get($accountKey);
$ipTimes = Cache::get($ipKey);
if ($accountTimes >= 1) {
$data = array('code' => 400, 'message' => '您的账号已被暂时锁定,请稍后再试', 'data' => '');
break;
}
if ($ipTimes >= 100) {
$data = array('code' => 400, 'message' => '您尝试的次数过多,账号已被暂时锁定,请稍后再试', 'data' => '');
break;
}
$data = array('code' => 400, 'message' => '您输入的密码及账户名不匹配,是否<a href="' . Helpers::url('/passport/back/index') . '" target="_blank">忘记密码?</a>', 'data' => '');
break;
}
... ...
... ... @@ -48,8 +48,7 @@ class RegisterController extends WebAction
$ip = Helpers::getClientIp();
$data = array('code' => 400, 'message' => '', 'data' => '');
$ipKey = md5('ip_checkmobile_' . $ip);
$ipTimeKey = md5('ip_checkmobile_time_' . $ip);
PassportModel::increment($ipKey,$ipTimeKey,1, 0, 3600);
PassportModel::increment($ipKey,1, 0, 3600);
$ipTimes = Cache::get($ipKey);
do{
/* 判断是不是AJAX请求 */
... ... @@ -131,8 +130,7 @@ class RegisterController extends WebAction
}
//发送代码
$sendCodeKey = md5('send_code_' . $area . '_' . $mobile);
$sendCodeTimeKey = md5('send_code_time_' . $area . '_' . $mobile);
PassportModel::increment($sendCodeKey,$sendCodeTimeKey, 1, 0, 3600);
PassportModel::increment($sendCodeKey,1, 0, 3600);
$sendCodeTimes = Cache::get($sendCodeKey);
if ($sendCodeTimes > 50) {
$data['message'] = '发送验证码太多';
... ...