简体   繁体   English

将相同的表单提交限制为每天一次?

[英]Limit identical form submissions to one per day?

I'm trying to code a very simple form in HTML and PHP. 我正在尝试用HTML和PHP编写一种非常简单的形式。 This isn't something I really know how to do, I'm trying to learn as I go, so sorry that this question is probably very basic and stupid. 这不是我真正知道的方法,我想边走边学,所以很抱歉这个问题可能非常基础和愚蠢。

I run a website where users can vote for our site on some directories, which helps the site grow. 我经营一个网站,用户可以在某些目录中为我们的网站投票,这有助于网站的发展。 In order to motivate them to do that, I need to log people's votes. 为了激励他们这样做,我需要记录人们的投票。 We've been doing this by having them submit a form with their username in it, but due to changes with the form service we were using, I'm now stuck trying to make this from scratch. 我们一直在通过让他们提交带有用户名的表单来进行此操作,但是由于我们使用的表单服务发生了变化,因此我现在一直试图从头开始。

Is there any way that I can limit people to only being able to submit their username once per 24 hours? 有什么办法可以限制人们每24小时只能提交一次用户名? This is how often they can vote for the site, and people have been complaining that they often forget if they've already voted or not. 这就是他们投票该网站的频率,人们一直抱怨说,他们常常忘记自己是否已经投票。 It would be nice if it would just deny their entry if they have already voted in a given day. 如果他们只是在某一天已经投票而拒绝他们的进入,那就太好了。

I don't know if this is even possible, trying to research how to do it only gives me how to limit the total number of submissions a form can accept before shutting down, which is not what I want. 我什至不知道这是否可能,尝试研究如何做到这一点只能让我如何限制表单在关闭之前可以接受的提交总数,这不是我想要的。

Here is the code that I have so far: 这是我到目前为止的代码:

    <HTML><HEADER><h1>Voting Validation - Top Site List</h1></HEADER><BODY>
<br>
<p>Input your username and click the button to vote!</p> 
<br>
<FORM ACTION="send-mail1.php" METHOD="POST">
<label for="Username">Username:</label>
<INPUT TYPE="TEXT" ID="Username" NAME="Uaername" SIZE=20></FORM>
<br>
<form action="http://www.top-site-list.com/roleplaying/vote/462074" target="_blank">
    <input type="submit" value="Vote!" />
</form></BODY></HTML>


<?php
    $mail_to = 'me@fake-email.com'; // specify your email here

    // Assigning data from the $_POST array to variables
    $Username = $_POST['Username'];

 //E-mail subject

 $Subject = 'Vote Log 1';

 //E-mail body

 $body_message = $Username "\r\n";

 <?php
    }
?>

Your website needs to somehow know, whether a particular user voted in the last 24 hours or not. 您的网站需要以某种方式知道特定用户是否在过去24小时内进行了投票。 Therefore it needs to store that piece of information somewhere, preferably in database. 因此,它需要将该信息存储在某个位置,最好存储在数据库中。

It's simple, you just have to know some basic SQL commands ( INSERT , UPDATE , SELECT ) and learn how to use it in PHP (there are many tutorials online). 很简单,您只需要了解一些基本的SQL命令( INSERTUPDATESELECT )并学习如何在PHP中使用它(在线上有很多教程)。

You could create a table votes with columns username and last_vote (the latter will store a timestamp of the last vote of a username). 您可以使用usernamelast_vote列创建表格votes (后者将存储用户名最​​后一次投票的时间戳)。 That's the SQL part. 那就是SQL部分。

Now, every time somebody posts the form, you need to check whether specified username is in that table, and if there is, then check if its last vote was more than 24 hours ago. 现在,每次有人发布该表单时,您都需要检查指定的用户名是否在该表中,如果存在,则检查其最后一次表决是否在24小时之前。 Finally, you need to update last_vote with current time (or insert a whole new row into table, if a username wasn't previously in database). 最后,您需要使用当前时间更新last_vote (或者,如果数据库中以前没有用户名,则在表中插入一个新行)。

The above example is for a standalone script (for simplicity), but of course you could integrate something similar directly to your CMS, which most likely already has table users , that can be used to store additional data (which is what was suggested in comments). 上面的示例是一个独立的脚本(为简单起见),但是您当然可以直接集成与CMS类似的东西,后者很可能已经具有表users ,可以用来存储其他数据(这在注释中建议) )。

edit: btw. 编辑:顺便说一句。 you have typo NAME="Uaername" 你有错字NAME="Uaername"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM