Results 1 to 9 of 9

Thread: PHP mail, cookies, fill out a form once only

  1. #1
    Join Date
    Oct 2007
    Posts
    43
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default PHP mail, cookies, fill out a form once only

    I'm creating a survey form, the results of which are e-mailed, and I've been asked to set it so that no one can fill it in twice.

    Obviously this is impossible, as it's a public survey and I'm not allowed to make people log in before filling it in. However, I know there are ways to limit the number of times someone can complete the form using cookies, and any tips, or links pointing to a tutorial would be appreciated.

    Edited to add: I've done a bit more research, and I need a persistent web cookie, which expires in one month. If someone had already filled in the form, it wouldn't be visible and they would get a 'you have already filled out this form' message instead.
    Last edited by marynorn; 06-23-2008 at 03:30 PM.

  2. #2
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    Well, I don't know about that, so sorry if this doesn't help; but, I am thinking that you should just store the IP address in a MySQL table when the guest submits the survey. If the guests IP is already in the table, you send them the message.

    Also, as far as I know, there is no such thing as an "invisible cookie."
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

  3. #3
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    Okay, here are two samples:

    1: Cookie method (not invisible, but it works )
    PHP Code:
    <?php

    if(isset($_COOKIE['survey'])){
        die(
    "You already sent in the survey");
    }else{
        
    //process form
        
    setcookie("survey"'set'time()+(60*60*24*30)); //cookie expires in one month
    }

    ?>
    2:MySQL method:
    PHP Code:
    <?php

    $ip 
    $_SERVER['REMOTE_ADDR'];

    //connect to MySQL
    $rows MySQL_Query("SELECT * FROM sent WHERE ip=\"$ip\";");

    if(
    MySQL_Num_rows($rows) > 0){
        die(
    "You already sent in the survey");
    }else{
        
    //process form
        
    MySQL_Query("INSERT INTO sent VALUES (\"$ip\");");
    }

    //close mysql connection

    ?>
    The difference is that the information in number 1 is store on the user's computer, number 2 is on the server. In number 1, if the user clears their cookies, they can resubmit the form. In number 2, if they have a dynamic IP (most people do), if they reset their router, they can resubmit the form.
    Last edited by Jas; 06-25-2008 at 05:21 PM.
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    The first one will not work because your not setting a cookie called survey, but TestCookie. Thus it will not work.
    Jeremy | jfein.net

  5. #5
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    Heh. That is funny. In my haste to produce a sample, I pasted in the function rather than actually writing it (saved my a whole 30 seconds ), and then I forgot to make the changes.

    Thanks Nile, it's fixed now.
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

  6. #6
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Also for set you used single quotes but for the cookie name you used double. Also you don't need to use 'set' just make it true.
    Here's the code:
    PHP Code:
    <?php
    if($_COOKIE['survey']){
        die(
    "You already sent in the survey");
        return 
    false;
    }else{
        
    //process form
        
    setcookie("survey"truetime()+(60*60*24*30)); //cookie expires in one month
        
    return true;
    }
    ?>
    Last edited by Nile; 06-26-2008 at 01:45 PM.
    Jeremy | jfein.net

  7. #7
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    @Nile-- please don't make pointless corrections to code. My code worked fine (double and single quotes do not matter, they both create a string; it's more politically correct to use double quotes, but there is no difference), and I know I didn't need the word set, I did that to show that is has been set. Do not correct working code, especially an example.
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

  8. #8
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    it's more politically correct to use double quotes, but there is no difference)
    Actually, it is better to use single quotes as it takes less time to parse. Just thought you should know that.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  9. #9
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    Yes, and no. In most cases, it is better to use single quotes, however for things like file names, double quotes are more politcally correct (I don't know why, because as you said it is slower). That is what I was refering to, not strings in general.
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

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
  •