php 提交后如何限制每次表单提交

bweufnob  于 2023-02-03  发布在  PHP
关注(0)|答案(6)|浏览(153)

我在谷歌上搜索如何限制用户每次提交。我有一个表单,用户不断提交他们的信息。它工作正常,但我怎么能限制用户每隔一天提交一次表单,例如,如果用户今天提交,他试图在几个小时后提交表单,他不能,直到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)){///这样它就可以限制它,但是我不知道它是否有效或者哪种方法是最好的

kqqjbcuj

kqqjbcuj1#

这取决于您在后端的实现,但逻辑上您希望允许用户提交表单,然后在服务器上检查当前时间与他们上次输入的时间戳的比较,如果差值小于24小时,则返回一个400错误请求并在客户端处理。

p8h8hvxi

p8h8hvxi2#

在加载请求的表单之前,应检查用户的请求是否早于24小时。此外,还应在数据库的requests表中添加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中为提交请求表格后:

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.
我希望我对您的问题有所帮助。如果出现问题,或者您需要更多帮助,请告诉我。其他有用链接:

dfuffjeb

dfuffjeb3#

最简单的方法是在存储特定用户的表单数据时将日期时间存储在表中,因此在插入新记录之前,您需要检查过去24小时内是否有相同用户ID的条目,然后优雅地向他/她显示消息,否则继续正常提交表单。

0mkxixxg

0mkxixxg4#

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;
    }
}
?>

错误是否仍然出现?请告诉我!

xbp102n0

xbp102n05#

背景:我制作了一个隐藏表单,以便在特定时间自动存储用户数据,但是,由于我输入它的时间,在一分钟内每秒钟为每个用户提交一次表单,因此导致每个用户每分钟有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类型值的数组偏移量),因为我的数据库设置为自动添加日期戳,而不是从表单中获取。如果您通过表单添加它,它应该不会给予您错误。

rfbsl7qr

rfbsl7qr6#

我能够解决我的请求。基本上我需要限制访问请求表的期间。示例表单是今天提交的,用户将无法访问它,直到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。
希望它能帮助别人在未来如果是这样的话,请竖起大拇指将不胜感激

相关问题