Add spam protection to my form? 1+2=3 ?

Discussion in 'Programming' started by lithman, Jun 29, 2007.

  1. #1
    I have form on my companies website that isn't being attacked my spammers.

    I am looking for a simple piece of code to put in the form above the submit button. Something simple like "1+2= ..." then they have to put in 3, or the form rejects. If the put in the correct answer, then the form submits.

    How can I do this?
     
    lithman, Jun 29, 2007 IP
  2. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Depends....

    1) are you wanting the "test" to be static or dynamic (ie is it always 1 + 2 or is it RandomNumber1 RandomMathsFunction RandomNumber2 = ...?)

    2) What serverside language are you using? PHP? .Net?
     
    AstarothSolutions, Jun 29, 2007 IP
  3. lithman

    lithman Well-Known Member

    Messages:
    1,189
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    130
    #3
    we're using php ..

    And the test can be static.
     
    lithman, Jun 29, 2007 IP
  4. ZenOswyn

    ZenOswyn Peon

    Messages:
    50
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It's usually done by generating 2 random numbers (usually between 1 and 20) and then adding them together. Then, storing a hash of the answer in a session. When the form is submitted, check the form input (hashed) against the session variable.
     
    ZenOswyn, Jun 29, 2007 IP
  5. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #5
    
    $questions[] = array('question','answer');
    $questions[] = array('question','answer');
    $questions[] = array('question','answer');
    
    //form start
    $x = rand(0,count($questions)-1);
    echo $questions[$x][0];
    echo '<input type="text" id="ans" name="ans"  value="">';
    echo '<input type="hidden" id="q" name="q" value="$x">';
    //form end
    
    //In your submit validation
    
    $pos = $_POST['q'];
    if($_POST['ans'] != $questions[$pos][1]){ die('Invalid answer');}
    
    PHP:
    Peace,
     
    Barti1987, Jun 30, 2007 IP