...

View Full Version : Javascript form submit issue



andyy15
05-02-2006, 03:52 PM
Hi everyone,
I have a form in which I want to enable a type of flood control where a user can only submit x amount of (5) times in a given period (1 hour). I came up with this code which uses cookies and javascript to attempt to solve this issue.


function AllowNoDups()
{
addtime = 60 * 60 * 1000 // 1 hour
expdate = new Date()
expdate.setTime(expdate.getTime() + addtime)
expdate = expdate.toGMTString()

var cookie_ls = document.cookie;
if (cookie_ls.indexOf(document.location) > -5)
{
alert("You can only submit 5 times in 1 hour. Please wait 1 hour before you submit again.");
return false;
}
else
{
document.cookie = window.location.href + " from " + document.referrer + "; path=/; expires=" + expdate;"";
return true;
};
};

The main concern would be to somehow delete/disable the cookie after 1 hour. After testing the above code I found that after 1 hour it would not let me submit again. Is there something wrong with the coding? Is there another way to do this using IP's and PHP? Using the cookie solution might not work if the user disables cookies.

I was trying out this php code to try to solve this issue in a different way using IP's and PHP:

$c = "SELECT * from `table_data` WHERE ip = '".$_SERVER['REMOTE_ADDR']."'";
$c2 = mysql_query($c);
while($c3 = mysql_fetch_object($c2)) {
$difference = date() - $c3->date;
if($difference < 60) die('<u>ALERT:</u>Please wait 1 hour before you submit again<BR>');
} //end while
where in table_data I have both the IP and date (in the form of 0000-00-00 00:00:00) stored after the submit button is pressed. This did not work. Also this code does not take into account how many times the user hits submit only takes into account the time.

Any suggestion with either the top code or the bottom code or a completely different solution would be greatly greatly appreciated.

Thanks :)

vwphillips
05-02-2006, 08:10 PM
<script language="JavaScript" type="text/javascript">
<!--
// Form Compendium f19_Part2 (12-05-2005)
// Form Cookie
// by Vic Phillips http://www.vicsJavaScripts.org.uk

var f19_Cookie='Fred';
var f19_Days=1/24/30; // 2 minutes

// Functional Code

// No Need To Change ***************************
var f19_SLString,f19_,f19_exp,f19_st,f19_len,f19_end,f19_st;

var f19_Exp=new Date(new Date().getTime()+f19_Days*86400000).toGMTString();


function f19_GetCookie(name) {
var f19_st=document.cookie.indexOf(name+"=");
var f19_len=f19_st+name.length+1;
if ((!f19_st)&&(name != document.cookie.substring(0,name.length))) return null;
if (f19_st==-1) return null;
var f19_end=document.cookie.indexOf(";",f19_len);
if (f19_end==-1) f19_end=document.cookie.length;
return decodeURI(document.cookie.substring(f19_len,f19_end));
}

function f19_SetFormCookie(value){
f19_SLString=f19_GetCookie(f19_Cookie)*1;
if (!f19_SLString){ f19_SLString=0; }
f19_SLString++;
document.cookie=f19_Cookie+"="+encodeURI(f19_SLString)+";expires="+f19_Exp+";path=/;"
if (f19_SLString>5){ alert('Wait 2 Minutes'); return false; }
return true;
}



//-->
</script>

andyy15
05-03-2006, 12:39 AM
Thanks vwphillips for your reply. Two questions, how do we change this from 2 minutes to 1 hour? Second how do we implement the script into the form?
Do we include <body onload="f19_GetFormCookie();" onunload="f19_SetFormCookie();" > in the body? What code do we include in the form input tag?
Thanks,
Andrew



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum