我正在搜索如何限制用户每次提交的内容。我有一个表单,用户不断提交他们的信息。它工作正常,但是如何限制用户每隔一天提交表单,例如,如果用户今天提交并且他试图在几个小时之后提交表单,直到24小时结束。
// attempt insert query execution
$sql = "INSERT INTO request (fname, lname, amount, cedula,user_id, category, points,comments ) VALUES ('$_POST[fname]', '$_POST[lname]', '$_POST[amount]','$_POST[cedula]','$user_id' ,'$user_cat','$points','$_POST[comments]' )";
if(mysqli_query($link, $sql )){
header("Location: http://www.loan2center.com/users/submitthankyou.php");
echo "Records added successfully.";
}
else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
这是我正在考虑的帖子,如果我声明$ DATE = date(“Y / m / d”);然后修改if(mysqli_query($ link,$ sql&& $ date< = 24000)){///所以这样它可以限制它,但我不知道它是否会起作用或者哪个是最好的方法此
答案 0 :(得分:1)
这取决于您在后端的实现,但从逻辑上讲,您希望允许用户提交表单,然后在您的服务器上,检查当前时间与其上一个条目的时间戳相比较。如果差异小于24小时,则返回400错误请求并在客户端处理。
答案 1 :(得分:0)
在加载请求表单之前,您应该检查用户是否在24小时之前请求了。此外,您应该在数据库的请求表中添加一个DATETIME类型的列(在下面的代码中命名为latest_request),以获取最新请求的时间和日期。
在您的php中填写申请表:
<?php
$user = --- ; // Give the $user variable the id of the current user.
$sql = "SELECT user_id, latest_request FROM requests WHERE user_id='$user'"
$query = $mysqli->query($sql);
$result = $query->fetch_assoc();
$can_request = 0; // Will be 0 if user has requested within 24 hours, 1 otherwise
if ($result['latest_request']!='')
{
$cur_datetime = new DateTime('now');
$latest_request = new DateTime($result['latest_request']);
$diff = $cur_datetime->diff($latest_request);
$diff_in_days = intval($diff->format('%d'));
if ($diff_in_days > 0)
$can_request = 1;
}
if ($can_request)
{
?>
// Put here your Request form
<?php
}
else
{
echo "Sorry, you can only request one day after your last request.";
}
?>
在您的php for after请求表单中提交:
date_default_timezone_set("/* Put here your continent/city, eg Europe/London */");
$latest_request = (string)date("Y-m-d H:i:s");
$sql = "INSERT INTO requests (fname, lname, amount, cedula,user_id, category, points,comments, latest_request) VALUES ('$_POST[fname]', '$_POST[lname]', '$_POST[amount]','$_POST[cedula]','$user_id' ,'$user_cat','$points','$_POST[comments]' )";
if(mysqli_query($link, $sql ))
{
header("Location: http://www.loan2center.com/users/submitthankyou.php");
echo "Records added successfully.";
}
else
{
echo "ERROR: Could not execute $sql. " . mysqli_error($link);
}
在这里,您可以找到php支持的时区列表:http://php.net/manual/en/timezones.php。
我希望我帮助你解决问题。如果出现问题,或者如果你想要更多东西,请告诉我。其他有用的链接:
答案 2 :(得分:0)
最简单的方法是在存储特定用户的表单数据时将日期时间存储在表中,因此在插入新记录之前,您需要检查同一用户ID是否存在条目过去24小时,然后优雅地向他/她显示信息,否则继续正常提交表格。
答案 3 :(得分:0)
Herre是完整的代码,对我来说很好用:
<?php
$mysqli = new mysqli("localhost", "root", "", "test"); // Complete this with your database data.
mysqli_set_charset ( $mysqli , "utf8" );
$user = 1 ; // Give the $user variable the id of the current user.
if (!isset($_POST["submit"]))
{
$sql = "SELECT user_id, latest_request FROM requests WHERE user_id='$user' ORDER BY latest_request DESC";
$query = $mysqli->query($sql);
echo $mysqli->error; // Use this to see if there is an error in your SQL query.
$result = $query->fetch_assoc();
$can_request = 0; // Will be 0 if user has requested within 24 hours, 1 otherwise
if ($result['latest_request']!='')
{
$cur_datetime = new DateTime('now');
$latest_request = new DateTime($result['latest_request']);
$diff = $cur_datetime->diff($latest_request);
$diff_in_days = intval($diff->format('%d'));
if ($diff_in_days > 0)
$can_request = 1;
}
else
{
$can_request = 1;
}
if ($can_request)
{
?>
<form action="" method="post">
<input name="fname" type="text"><br><br>
// Add here all the other inputs you want<br><br>
<input name="submit" type="submit">
</form>
<?php
}
else
{
echo "Sorry, you can only request one day after your last request.<br>";
echo "You requested at: ".$latest_request->format('Y-m-d H:i:s');
}
}
else
{
date_default_timezone_set("/* Put here your continent/city, eg Europe/London */");
$cur_datetime = (string)date("Y-m-d H:i:s");
// In the code below, add all the data you want to save in the table (fname, lname, amount, cedula, category, points, comments)
$sql = "INSERT INTO requests (user_id, latest_request) VALUES ('$user','$cur_datetime')";
$mysqli->query($sql);
if ($mysqli->error == "")
{
echo "OK";
header("Location: http://www.loan2center.com/users/submitthankyou.php");
}
else
{
echo $mysqli->error;
}
}
?>
错误是否仍然出现?请让我知道!
答案 4 :(得分:-1)
我能够解决我的要求。 基本上我需要按期间限制对请求表单的访问。今天提交了示例表单,用户将在3天后才能访问它,并且创建新用户时也可以访问请求表单。这是我的代码
<?php
// will declare variable
$current_now = date("Y-m-d H:i:s");
$last_request = $request_last_date->req_date; // last submitted date on data base(hidden field within the form)
$dtestart = new DateTime($current_now);
$dteEnd = new DateTime($last_request);
$dteDiff = $dteEnd ->diff($dtestart);
$access = 224;
if ($dteDiff->format('%d%h%') > $access )
{echo "u can access now";//>? form <?php
}elseif ($dteDiff->format('%d%h%') == 00 )
{
echo "if new user also access";//>? form <?php
}
else {
echo " u wait , no access";
}
>?
它是这样做的,因为对于没有提交任何表单的新用户,last_request日期为“0”,并且代码设置为3以上的最后日期以便访问。
日期差异我使用天数和小时作为1个示例224 = 3天恰好2天24小时,这就是$ access = 224的原因。
希望将来有人帮助某人,请赞成赞赏