[英]How to limit form submition per time after submit
我正在谷歌搜索如何限制用戶每次提交。 我有一個表格,用戶可以不斷提交他們的信息。 它工作正常,但我如何限制用戶每隔一天提交一次表單,例如,如果用戶今天提交並且他試圖在幾個小時后提交表單,他必須等到 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)){/// 這樣它就可以限制它,但我不知道它是否有效或者哪個是最好的方法
這取決於您在后端的實現,但從邏輯上講,您希望允許用戶提交表單,然后在您的服務器上,檢查當前時間與其上一個條目的時間戳相比較。 如果差異小於24小時,則返回400錯誤請求並在客戶端處理該錯誤請求。
在加載請求表單之前,您應該檢查用戶是否在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 。
我希望我幫助你解決問題。 如果出現問題,或者如果你想要更多東西,請告訴我。 其他有用的鏈接:
最簡單的方法是在存儲特定用戶的表單數據時將日期時間存儲在表中,因此在插入新記錄之前,您需要檢查在過去24小時內是否有相同用戶ID的條目然后優雅地向他/她顯示消息,否則繼續正常提交表格。
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;
}
}
?>
錯誤是否仍然出現? 請告訴我!
背景:我做了一個隱藏的表單來在特定時間自動存儲用戶數據,但是,由於我輸入它的時間,每秒鍾為每個用戶提交表單一分鍾,因此導致每個用戶每分鍾 60 個條目。
為了解決這個問題,我使用了@Thanasis1101 提供的部分代碼。 我使用了發布記錄的常規方法,但在運行INSERT INTO table (columns) VALUES (record_values)
腳本之前,我添加了以下內容:
$sql = "SELECT * FROM user_stats WHERE id='$id' ORDER BY date DESC";
$query = $conn->query($sql);
echo $conn->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['date'] !=''){
$cur_datetime = new DateTime('now');
$date = new DateTime($result['date']);
$diff = $cur_datetime->diff($date);
$diff_in_hours = intval($diff->format('%h'));
if ($diff_in_hours > 12)
$can_request = 1;
}
else {
$can_request = 1;
}
if ($can_request){
這使得表單在最后一次提交后的 12 小時內根本無法提交。 但是,出於某種原因,我必須在行中添加if ($result['date'] !=''){
用戶第一次提交數據時,它會添加記錄但會拋出錯誤(PHP 警告:嘗試訪問數組偏移類型 null 的值),因為我的數據庫設置為自動添加日期戳,而不是從表單中獲取它。 如果您通過表單添加它,它不應該給您錯誤。
我能夠解決我的要求。 基本上我需要按期間限制對請求表單的訪問。 今天提交了示例表單,用戶將在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。
如果有的話,希望它能在將來幫助某人,請大家贊賞
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.