SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 32

Hybrid View

  1. #1
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Limiting Number of Form Submissions?

    I'm using the script below to limit a visitor at my site to only be able to post a form 5 times. I believe the reason I've received more e-mails is because the visitor closes their browser then opens a new one and they can post two times each time they do this.

    PHP Code:
    <%
    count Request("count")
    if 
    count "" Then 
    count 
    0
    Else
    count count 1
    End 
    If
    %>

    <%
    If 
    count 5 Then
    %>
    <
    a name="step5"></a><input type="submit" name="Submit" value="Submit">
    <%
    Else
    %>
    This form has been submitted 5 timesPlease contact me for more information
    Can somebody show me how I can do this so it will only work twice within a 24 hour period even if the visitor closes their browser and opens a new one? I would think cookies be the answer to this but what happens if the visitor has cookies turned off? If this would be a good solution can somebody please help me out? I'm not familiar with cookies.

    Any help would be greatly appreciated.
    John Saunders

  2. #2
    Drupaler bronze trophy greg.harvey's Avatar
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    3,258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cookies would be the only way to do it if you weren't asking users to log in. If you're dealing with registered users however, then you can keep record in a database of how often they've submitted the form.

    To go the cookies route, you'd have to fire a "Cookies must be switched on to use this site" message if your cookies are returning NULL values (ie: aren't setting properly).

    G

  3. #3
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Greg,

    Thanks for the info. I don't have a user login section so I would have to go with the second option. Could you give me an example of what I need to add to my page to get this to work? I haven't really used cookies before and I'm not sure how to do this.
    John Saunders

  4. #4
    Drupaler bronze trophy greg.harvey's Avatar
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    3,258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure -- it's dead easy. To limit to 2 submissions a day using similar code to the above, you'd do something like:

    Code:
    <%
    count = Request.Cookies("count")
    
    '### If the cookies has a NULL value write a value to it
    if count = "" Then
    Response.Cookies("count") = 0
    Response.Cookies("count").Expires = Date + 1   '# wipes the cookie after a day
    End If
    
    '### Re-populate the count variable
    count = Request.Cookies("count")
    
    '### if it's still NULL then cookies must be off
    if count = "" Then
    %>
    You appear to have cookies switched off.
    You need cookies on to use this site.
    <%
    Else 
    count = CInt(count) + 1   '# use CInt as it will be a string from the cookie
    Response.Cookies("count") = count
    Response.Cookies("count").Expires = Date + 1
    End If 
    %> 
    
    <% 
    If count < 3 Then 
    %> 
    <a name="step5"></a><input type="submit" name="Submit" value="Submit"> 
    <% 
    Else 
    %> 
    This form has been submitted 2 times. Please contact me for more information.
    G

  5. #5
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Greg, thanks a lot! I tested it out and it is working great. However, I just noticed another way a visitor could submit the form over and over again without being limited to only a few posts.

    If I hit back and re-enter values, it keeps working and it sends me an e-mail each time I do this. Is there a way to expire the page if the visitor hits the back button? The script I am using just posts to the same ASP file, in case this makes a difference.

    Any ideas on how to put a stop to this? I think expiring the page in the headers would work, but I only know how to do this in PHP. I tried adding the code that will work in PHP to the top of the ASP file but it came up with a 500 error:

    header("Cache-Control: no-cache, must-revalidate");
    header("Pragma: no-cache");
    header("Expires: Mon,26 Jul 1997 05:00:00 GMT");

    Thanks again.
    John Saunders

  6. #6
    Drupaler bronze trophy greg.harvey's Avatar
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    3,258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    Code:
    <%
    Response.AddHeader "Pragma", "no-cache"
    Response.AddHeader "cache-control", "no-store"
    %>

  7. #7
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tested it out with cookies disabled in the browser and for some reason it isn't showing the message that the visitor needs to have cookies turned on. It shows the submit button and allows the visitor to submit the form as many times as they like instead of being limited to a few.

    I have this in my page where the main text starts beneath my header section:

    <%
    count = Request.Cookies("count")

    '### If the cookies has a NULL value write a value to it
    if count = "" Then
    Response.Cookies("count") = 0
    Response.Cookies("count").Expires = Date + 1 '# wipes the cookie after a day
    End If

    '### Re-populate the count variable
    count = Request.Cookies("count")

    '### if it's still NULL then cookies must be off
    if count = "" Then
    %>
    You appear to have cookies switched off.
    You need cookies on to use this site.
    <%
    Else
    count = CInt(count) + 1 '# use CInt as it will be a string from the cookie
    Response.Cookies("count") = count
    Response.Cookies("count").Expires = Date + 1
    End If
    %>

    then past my form I have:

    <%
    If count < 3 Then
    %>
    <a name="step5"></a><input type="submit" name="Submit" value="Submit">
    <%
    Else
    %>
    This form has been submitted 2 times. Please contact me for more information.
    <%End If%>


    Any ideas?
    John Saunders

  8. #8
    Drupaler bronze trophy greg.harvey's Avatar
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    3,258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you tried actually writing the value of your cookie back to the screen to make sure it's not running on an old cookie or something. It may be that your cookie is there from when you tried it with cookies on, but now that cookies are off, it's still reading the existing cookie though not allowing you to alter it .... Have a check:

    Code:
    Current cookie value = <%=Request.Cookies("count") %>
    G

  9. #9
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm testing it on two computers both using IE 6. On the one that I had already accepted the cookie, it says the value is 7. On the one that I disabled cookies on it just says Current cookie value = (without a number).
    John Saunders

  10. #10
    Drupaler bronze trophy greg.harvey's Avatar
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    3,258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok. Replace the line
    Code:
    if count = "" Then
    with
    Code:
    if count = Nothing Then
    in both occurences and see what happens.

    G

  11. #11
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When I replaced both instances of the old if count with the new stuff it came up with:

    Microsoft VBScript runtime error '800a005b'

    Object variable not set

    /scripts/calculator.asp, line 315

    Do you know why it isn't printing:
    Your browser currently does not have cookies enabled. In order to use the calculator you will need to turn cookies on.
    ?
    John Saunders

  12. #12
    Drupaler bronze trophy greg.harvey's Avatar
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    3,258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmmmmmm .... not tried that code before -- just found it quickly and the actual syntax was in direct relation to a cookie. Try this. I've emboldened the changed line:

    Code:
    <%
    count = Request.Cookies("count")
    
    '### If the cookies has a NULL value write a value to it
    if count = "" Then
    Response.Cookies("count") = 0
    Response.Cookies("count").Expires = Date + 1   '# wipes the cookie after a day
    End If
    
    '### Re-populate the count variable
    count = Request.Cookies("count")
    
    '### if it's still NULL then cookies must be off
    if Request.Cookies("count") = Nothing Then
    %>
    You appear to have cookies switched off.
    You need cookies on to use this site.
    <%
    Else 
    count = CInt(count) + 1   '# use CInt as it will be a string from the cookie
    Response.Cookies("count") = count
    Response.Cookies("count").Expires = Date + 1
    End If 
    %> 
    
    <% 
    If count < 3 Then 
    %> 
    <a name="step5"></a><input type="submit" name="Submit" value="Submit"> 
    <% 
    Else 
    %> 
    This form has been submitted 2 times. Please contact me for more information.
    <%
    End If
    %>
    G

  13. #13
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I made the changes and it came up with another error like the one above, then I changed the part in bold to read = "" Then instead of = Nothing Then. It didn't come up with an error after I added that, but it isn't coming up with the no cookies message when the cookies are turned off.

    Any ideas?
    John Saunders

  14. #14
    Drupaler bronze trophy greg.harvey's Avatar
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    3,258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, beats the hell outa me. It should work fine (http://www.aspfree.com/authors/chris...llcookies.aspx) -- and that would be with if count = Nothing Then ...



    I read an article recently about handling NULL values and the quirks of VB/ASP and NULL, but I can't find it now ...

    G

  15. #15
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The "is Nothing" command is ASP.NET only, the equvilient in ASP would be isEmpty().

    So it would look like:
    [vbs]
    if isEmpty(Request.Cookies("count")) Then
    [/vbs]


  16. #16
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is really strange that it isn't working. I'm not sure why the = Nothing doesn't work but the = "" does, it looks like it means the same thing.

    I also can't figure out why it isn't printing the error if the count isn't found then. I'm not all that great at ASP, but the logic seems to be correct:

    <%

    code....

    '### if it's still NULL then cookies must be off
    if Request.Cookies("count") = "" Then
    %>

    print error

    <%
    Else
    count = CInt(count) + 1 '# use CInt as it will be a string from the cookie
    Response.Cookies("count") = count
    Response.Cookies("count").Expires = Date + 1
    %>

    print this instead
    ------
    form

    <%End If%>

    I just don't get it cause it seems like it should work. Thanks for all of your wonderful help though....I really appreciate it.
    John Saunders

  17. #17
    Drupaler bronze trophy greg.harvey's Avatar
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    3,258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by dhtmlgod
    The "is Nothing" command is ASP.NET only, the equvilient in ASP would be isEmpty().

    So it would look like:
    [vbs]
    if isEmpty(Request.Cookies("count")) Then
    [/vbs]

    Just tried that on my dev server for the hell of it, and IsEmpty stubbornly returns 'False' even if the cookie has been wiped ....

    This is not logical captain.

    G

  18. #18
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I GOT IT TO WORK!!!!!!! I changed the code around a little bit and got rid of the: if count = "" Then Response.Cookies("count") = 0 at the top of the script. I think the Response.Cookies("count") =, was making it not display the error message since it was at teh very top.

    Code:
    <%
    count = Request.Cookies("count")
    
    '### If the cookies has a NULL value write a value to it
    Response.Cookies("Count") = stCount
    Response.Cookies("Count").Expires = Date + 1 '# wipes the cookie after a day
    
    '### Re-populate the count variable
    stCount = Request.Cookies("Count")
    
    '### if it's still NULL then cookies must be off
    if Count = "" Then
    %>
    <p>You appear to have cookies switched off.
    You need cookies on to use this site.</p>
    <%
    Else
    Count = CInt(Count) + 1 '# use CInt as it will be a string from the cookie
    Response.Cookies("Count") = Count
    Response.Cookies("Count").Expires = Date + 1
    %>
    Greg....How did you quote the scripting in Visual Basic?

    Thanks for all your help! I appreciate it!
    Last edited by JohnSaunders; Sep 23, 2002 at 08:28.
    John Saunders

  19. #19
    Drupaler bronze trophy greg.harvey's Avatar
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    3,258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Erm .... I just left dhtmlgod's VBS tags in ....

    Glad it's working. Good stuff. That IsEmpty thing is going to bug me though!!

    G

  20. #20
    SitePoint Enthusiast
    Join Date
    Aug 2002
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Testing a common dimensioned variable against nothing causes an error as it expects an object, hence the 'Object variable not set' error. IsEmpty() checks for an uninitialised var and IsNull() checks for a var without any valid data.

    IMHO I don't think it is good practice to rely on a direct check against a variable, in this case the cookie, which may or may not exist as this is sure to return inonsistent results. It may be better to create a temporary var and populate with the value of the cookie (or not as the case may be?) then check for the value. This ensures the var to be checked has a value consistent with that saved in the cookie and could then theoretically be tested with IsNull().

    Ed.

  21. #21
    Drupaler bronze trophy greg.harvey's Avatar
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    3,258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah hah!! IsNull() .... cool. Thanks. I know this wasn't my problem, but you've just saved me some sleep ...

    G

  22. #22
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,200
    Mentioned
    105 Post(s)
    Tagged
    1 Thread(s)
    IsNull can get a little freaky-deaky on you too. I find this a much more reliable (and faster) check:

    [vbs]
    If Len(Trim(ObjectName)) = 0 Then
    [/vbs]
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style

  23. #23
    Drupaler bronze trophy greg.harvey's Avatar
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    3,258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah! Now I *DO* like that! Just checking the string length -- nice!

  24. #24
    SitePoint Evangelist Brandon Luhring's Avatar
    Join Date
    Apr 2002
    Location
    IN, USA
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sorry... but I can't really see how you've improved your original situation. Obviously, your visitors are trying pretty hard to submit more than twice a day. For them to open another browser to avoid your original script, proves that.

    Cookies give you no advantages in that case. All they have to do is delete the cookie. Very easy to do.

  25. #25
    Drupaler bronze trophy greg.harvey's Avatar
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    3,258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    *IF* they're smart enough to realise that cookies are being used and know how to delete them. Remember that 99% of Internet users are stupid. Fact! Just cos you know it's easy, doesn't mean everyone can do it. And it's as foolproof as you'll get without having some kind of registered users system.

    G

    Ps -- another fact -- 78.2% of statistics are made up on the spot -- Vic Reeves said that!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •