1. 规定重置的时间为一小时
@session_start();
$h =date('h');
if (!isset($_SESSION['count'])) $_SESSION['count'] = 0;
if (!isset($_SESSION['h'])) $_SESSION['h'] = $h;
if ($_SESSION['h'] !== $h) { //每一小时重置一次
$_SESSION['count'] = 0;
$_SESSION['h'] = $h;
}
2.当count小于2时执行提交操作,否则提示留言超过限制
//小于2次就执行
if ($_SESSION['count'] < 2) {
$data = Db::name('message')->insert(['name' => $name, 'phone' => $phone, 'email' => $email, 'title' => $title, 'content' => $content,]);
$_SESSION['count'] = $_SESSION['count'] + 1; //执行count+1;
if ($data) {
return json(['code' => 1, 'msg' => '留言成功,感谢您的留言', 'datas' => $data]);
} else {
return json(['code' => 0, 'msg' => '留言失败,请重试', 'datas' => $data]);
}
}else //count大于2时返回限制信息
{
return json(['code' => 400, 'msg' => '留言次数限制,请稍后重试',]);
}
3 效果展示
1条评论