Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-02-2006, 02:52 PM   PM User | #1
andyy15
New to the CF scene

 
Join Date: May 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
andyy15 is an unknown quantity at this point
Javascript form submit issue

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.

Code:
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:
PHP Code:
$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
andyy15 is offline   Reply With Quote
Old 05-02-2006, 07:10 PM   PM User | #2
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,354
Thanks: 3
Thanked 458 Times in 445 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
Code:
<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>
__________________
Vic

God Loves You and will never love you less.

http://www.vicsjavascripts.org.uk/

If my post has been useful please donate to http://www.operationsmile.org.uk/
vwphillips is offline   Reply With Quote
Old 05-02-2006, 11:39 PM   PM User | #3
andyy15
New to the CF scene

 
Join Date: May 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
andyy15 is an unknown quantity at this point
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
andyy15 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:52 PM.


Advertisement
Log in to turn off these ads.