Disable users from submitting the same ID more than once

AnonLeo

Regular Member
Joined
Nov 23, 2013
Messages
363
Reaction score
275
Hey,

So I have a form that users put an ID (e.g: 89123) and then they click submit, but there are some people who submit the SAME ID more than once and that makes the server a little slower, is there a way to not allowing them to submit the same ID more than one time?

In other words; Let's say a user writes "99432" at 9:00PM in the <input> of the form, he clicks "Submit" and then the process is done
He wants to submit the SAME ID (99432) again in the form at 9:10PM and then he clicks "Submit" and and then the process is done

What I want is a PHP code or something to disable the users from submitting the SAME ID if it's already submitted before


I hope I made myself clear :D Sorry for poor English
Thanks and I hope someone will help me with this problem :D
 
ID for what? Loop through the table where the IDs are stored and if that ID exists, throw an error, if not allow the ID.
 
I lied, you shouldn't need to loop. Just do a select query with the textbox value from that table and if it !=="" then allow the entry.
 
I think what you should do is to create a cookie with the login time as value.

Then, whenever the submit button is clicked, before submitting the form, the javascript will check the cookie, compare the time, then decide whether to allow submitting.

Added:
If you want to enable multiple id, just make sure the value stored in the cookie is unique to each id.
e.g.
[id1]-time
[id2]-time
 
What is the full code for that? (if you know) :D
 
do you know the code for that? PHP? :D
 
Here you go. Something like this should work. This is obviously after you have connected to SQL and selected the database though.

Code:
$result = mysql_query("SELECT 1 FROM teh_table WHERE `ID` = '$formID'");
if ($result && mysql_num_rows($result) > 0){
        echo 'ID Exists already!'; 
}else{
    echo 'ID Doesn't exist or there is a database connection issue. Error: ' . mysql_error();
}
 
SELECT COUNT(*) AS howmany FROM WHEREVER WHERE id = "$id"

Fetch the results. If row['howmany'] > 0 then disallow_signup()
 
I have tried it but id doesn't seem to work :/
 
Firstly, my interpretation of your question is you are trying to prevent the traffic from the user if the same id has been submitted for the last hour.

The difference with this is that this assumption should let us work towards preventing the submit and not after submitting, check if the submit is a duplicate one.

The below code is partly copied from hxxp://www.quirksmode.org/js/cookies.html
Thus, the credit goes to the original content creator

What I am doing is to check if the id has been set in the cookie?
If yes, that means it has been submitted and prevent the submission.
If not, set a cookie for the id that expires in 1 hour, and proceed with the submission.

Code:
<html>
	<head>
		<script src="http://code.jquery.com/jquery-latest.min.js"></script>
		
		<script>
		function createCookie(name,value) {
			var date = new Date();
			date.setTime(date.getTime()+(1*60*60*1000));
			var expires = "; expires="+date.toGMTString();

			document.cookie = name+"="+value+expires+"; path=/";
		}

		function readCookie(name) {
			var nameEQ = name + "=";
			var ca = document.cookie.split(';');
			for(var i=0;i < ca.length;i++) {
				var c = ca[i];
				while (c.charAt(0)==' ') c = c.substring(1,c.length);
				if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
			}
			return null;
		}

		function eraseCookie(name) {
			createCookie(name,"",-1);
		}

		jQuery(document).submit(function(event, data) 
		{ 
			var c = readCookie(jQuery("#id").val());
		
			
			if (null == c) {
				createCookie(jQuery("#id").val(), 'set');
			} else {
				alert("you have submitted this id within the last hour");
				return false;
			}
		}); 

		</script>		
	</head>
 
	<body>
		<form action="targeturl" method="POST" ><br/>
			id : <input type="id" id='id' value="1" /><br/>

			<input type="submit" value="Submit" />
		</form>
		

	</body>
</html>
 
Hi, what if I want to change it from 1 hour to 3 minutes?
 
Hi, what if I want to change it from 1 hour to 3 minutes?

If you want 3 minutes, basically, you just need to increase the date time setting
date.setTime(date.getTime()+(1*60*60*1000));
to
date.setTime(date.getTime()+(3*60*60*1000));

to increase it to days, e.g. 2 days
date.setTime(date.getTime()+(2*24*60*60*1000));

hth
 
If you want 3 minutes, basically, you just need to increase the date time setting
date.setTime(date.getTime()+(1*60*60*1000));
to
date.setTime(date.getTime()+(3*60*60*1000));

to increase it to days, e.g. 2 days
date.setTime(date.getTime()+(2*24*60*60*1000));

hth

I hope you don't mind me asking; How can I make a timer for the form when a user submits the form?
Explanation: Let's see a visitor comes to my website, he writes his ID and clicks submit, the process is done, now he wants to go back and submit the form again (Same ID or different ID sometimes), he submits again after 30 seconds. What I want is a code that will tell him "Please wait 3 minutes before submitting the form again" Instead of "You have submitted within the past hour".
Is there a JS code or something for that? :)
I hope you understand what can of code I am talking about :D
Thanks in advance
 
What you can do is to change the statement below

alert("you have submitted this id within the last hour");

to whatever message you wished to be prompt.

In this particular case, it will be
alert("Please wait 3 minutes before submitting the form again");

On a side note, you would surely want your message to tally with the length of time user should wait for.
i.e.
if you script set to check user to wait for 30 minutes, your message should state 30 minutes.

hth
 
Back
Top