按照这个代码账号登陆密码错误超过5次限制登陆10分钟,当一个账号限制后不同账号登陆时依旧会限制10分钟,该如何解决这个问题。
session_start(); // 开启 Session
$max_attempts = 5; // 最多尝试次数
$block_duration = 10 * 60; // 阻塞时间(单位:秒)
if (isset($_SESSION['attempts'])) {
$attempts = $_SESSION['attempts'];
} else {
$attempts = 0;
}
if (isset($_SESSION['last_attempt_time'])) {
$last_attempt_time = $_SESSION['last_attempt_time'];
} else {
$last_attempt_time = 0;
}
if ($attempts >= $max_attempts && time() - $last_attempt_time < $block_duration) {
$remaining_time = $block_duration - (time() - $last_attempt_time);
echo '您已经尝试了 ' . $attempts . ' 次,请在 ' . $remaining_time . ' 秒后再试。';
} else {
// 处理登录逻辑
if ($login_successful) {
// 登录成功,清空记录
$_SESSION['attempts'] = 0;
$_SESSION['last_attempt_time'] = 0;
} else {
// 登录失败,记录次数和时间
$_SESSION['attempts'] = $attempts + 1;
$_SESSION['last_attempt_time'] = time();
echo '账号或密码错误。';
}
}
可以在 $_SESSION['attempts'] 和 $_SESSION['last_attempt_time'] 之前添加一个对用户的唯一标识的检查,例如:
if (isset($_SESSION['user_id']) && $_SESSION['user_id'] == $user_id) {
// 之前已经有该用户的尝试记录,继续检查次数和时间间隔
// ...
} else {
// 新的用户登录,初始化尝试记录
$_SESSION['user_id'] = $user_id;
// ...
}
其中,$user_id 表示该用户的唯一标识,可以根据实际情况进行设置。这样就能够为不同的用户分别记录登录失败的次数和最后一次尝试时间了,不会因为不同用户的登录记录混淆而导致限制时间错误。
订单生成代码如下: Application\Home\Controller\PayController.class.php
public function submit() {
$paytype = I("post.paytype");
$data['order_money'] = I("post.money", 1);//订单金额
$data['order_no'] = date("YmdHis") . rand(1000, 9999);//订单号
$data['pay_type'] = $paytype;
$data['addtime'] = time();
M("order")->add($data);
$site_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
$dir = dirname($site_url);
$data['url_notify'] = $dir . "/Notify/pay_alipay";//回调地址
$data['url_return'] = $dir . "/Pay/order_detail";//返回地址
$data['title'] = "标题" . $data['order_no'];
$data['body'] = "主体内容" . $data['order_no'];
if ($paytype == 'alipay') {
$this->alipay_jump($data);
} elseif ($paytype == 'wechat_code') {
$data['url_notify'] = $dir . "/Notify/pay_weixin";
$this->wechat_jump($data);
}
}
支付成功回调,更改订单状态为已付款: Application\Home\Controller\NotifyController.class.php
public function pay_weixin() {
$simple = json_decode(json_encode(simplexml_load_string($GLOBALS['HTTP_RAW_POST_DATA'], 'SimpleXMLElement', LIBXML_NOCDATA)), true);
$notify_data['order_no'] = $notify_data['trade_no'] = $simple['out_trade_no'];
$notify_data['third_id'] = $simple['transaction_id'];
$notify_data['pay_money'] = $simple['total_fee'];
$notify_data['payment_method'] = 'weixin';
// $sign = $simple['sign'];
// file_put_contents('ac_simple.txt', json_encode($simple));
// file_put_contents('ac_notify_data.txt', json_encode($notify_data));
$this->order_pay($notify_data);
}
public function pay_alipay() {
$notify_data['order_no'] = $notify_data['trade_no'] = I("post.out_trade_no");
$notify_data['third_id'] = I("post.trade_no");
$notify_data['pay_money'] = I("post.total_fee");
$notify_data['payment_method'] = 'alipay';
$this->order_pay($notify_data);
file_put_contents('ac_notify_data.txt', json_encode($_REQUEST));
}
/**
* 支付结果返回
*/
public function order_pay($data_order) {
$order_no = $data_order['order_no'];
if ($order_no == '') {
return false;
}
$order_info = M('order')->where(array("order_no" => $order_no))->find();
if ($order_info['state'] == 0) {
$data_order['update_time'] = $_SERVER ['REQUEST_TIME'];
$data_order['state'] = 1; // 已付款
M('order')->where(array("order_no" => $order_no))->save($data_order);
}
}