FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Help required with Captcha script





welshsteve
Hi everyone. I'm still pretty new to PHP so am struggling to implement things at the moment.

At the moment I have a feedback form on a website which works really well. But due to a recent spate of "spam" via the form, I would like to implement a "captcha" script to force users to enter a confirmation code before allowing the form to be processed and emailed.

I have found a script at http://www.white-hat-web-design.co.uk/articles/php-captcha.php but am struggling to get it to work with my current form handling script. I'm not sure which bits go where basically.

Here is the code the website asks you to include on your form page.
Code:

<img src="CaptchaSecurityImages.php" />
Security Code:
<input id="security_code" name="security_code" type="text" />


I've done this ok just by adding it within the form code on the website.

You're then asked to include the below in your form handling script.

Code:

<?php
   session_start();
   if(($_SESSION['security_code'] == $_POST['security_code']) && (!empty($_SESSION['security_code'])) ) {
      // Insert you code for processing the form here, e.g emailing the submission, entering it into a database.
      unset($_SESSION['security_code']);
   } else {
      // Insert your code for showing an error message here
   }
?>


Here is my current form handling script. Can anybody help me in re-arranging it to work with the above?

Code:

<?php
  $contactname = $_REQUEST['contactname'] ;
  $email = $_REQUEST['email'] ;
  $email2 = 'me@mydomain.com' ;
  $club = $_REQUEST['club'] ;
  $query = $_REQUEST['query'] ;
  $comments = $_REQUEST['comments'] ;

 
  if (!isset($_REQUEST['email'])) {
    header( "Location: http://www.ldbsa.co.uk/feedback.php" );
  }
  elseif (empty($email) || empty($club) || empty($query) || empty($comments) || empty($contactname)) {
echo '<h1>ERROR</h1><p>Not all fields have been filled in.  Please try again.</p><p><a href="feedback.php"><< Back to form</a></p>';
}
  else {
    mail( "$email, $email2", "Thank you for your feedback/questions",
          "<p>Name: $contactname</p>
        <p>E-Mail Address: $email</p>
        <p>Your club: $club</p>
        <p>Your query was regarding: $query</p>
        <p>Comments/Questions: $comments</p>",
        "To: $contactname <$email>\n" .
        "From: LDBSA <$email2>\n" .
        "MIME-Version: 1.0\n" .
        "Content-type: text/html; charset=so-8859-1");
    header( "Location: http://www.ldbsa.co.uk/thanks.php" );
  }
?>


My problem is the empty check part adds extra ifs to the equation and I'm worried that it'll all go wrong. The form processing bit is in sections in my code
- the variable setting
- the empty check
- the mail bit.

The error bit is the html after the empty check.

If anyone can get this working for me I'd greatly appreciate it, and will make a note of it all for future reference so I know how.
DanielXP
This code should work.

Code:
<?php
session_start();
$contactname = $_REQUEST['contactname'] ;
$email = $_REQUEST['email'] ;
$email2 = 'me@mydomain.com' ;
$club = $_REQUEST['club'] ;
$query = $_REQUEST['query'] ;
$comments = $_REQUEST['comments'] ;
 
   if (!isset($_REQUEST['email'])) {
      header( "Location: http://www.ldbsa.co.uk/feedback.php" );
   } else if(($_SESSION['security_code'] != $_POST['security_code']) || (empty($_SESSION['security_code']))) {
      echo '<h1>ERROR</h1><p>invalid security code.  Please try again.</p><p><a href="feedback.php"><< Back to form</a></p>'; 
   } else if (empty($email) || empty($club) || empty($query) || empty($comments) || empty($contactname)) {
      echo '<h1>ERROR</h1><p>Not all fields have been filled in.  Please try again.</p><p><a href="feedback.php"><< Back to form</a></p>';
   } else {
      mail( "$email, $email2", "Thank you for your feedback/questions",
          "<p>Name: $contactname</p>
        <p>E-Mail Address: $email</p>
        <p>Your club: $club</p>
        <p>Your query was regarding: $query</p>
        <p>Comments/Questions: $comments</p>",
        "To: $contactname <$email>\n" .
        "From: LDBSA <$email2>\n" .
        "MIME-Version: 1.0\n" .
        "Content-type: text/html; charset=so-8859-1");
      header( "Location: http://www.ldbsa.co.uk/thanks.php" );
   }
?>
welshsteve
Thanks I'll give it a try.
Related topics
A great website poll....
Flash - Photo Gallery - need help please.
Need help with php script
Need help making logout script
Need help with PHP script[RESOLVED]
Need help with logger script. (half done)
need help with a script
need help with a script for vb its not to hard i think
help required to create ads like www.rediff.com
Sending simple emails using PERL - Help required
Domain name redirection MX entries help required
Help! This small script has logical problems. Please help
Need Help. Post - Mail script failure
Serious help required!
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.