简体   繁体   中英

Limit identical form submissions to one per day?

I'm trying to code a very simple form in HTML and 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? 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. 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).

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). That's the SQL part.

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. 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).

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).

edit: btw. you have typo NAME="Uaername"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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