integrating captcha on my contact form

how to integrate captcha script on contact form?

Page 1 of 1

2 Replies - 6004 Views - Last Post: 03 August 2009 - 06:41 PM Rate Topic: -----

#1 mobius19  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 26-November 08

integrating captcha on my contact form

Posted 03 August 2009 - 09:29 AM

Hello everyone, i am very new to php and i need your help. I have a contact form which i generated from the web. it's very good because it really sends data but i need to integrate a captcha code so i will get less spam. my code for the html form is this:
<form method="POST" action="contact.php">
Fields marked (*) are required

<p>Name:* <br>
<input type="text" name="Name">
<p>Telephone:<br>
<input type="text" name="Telephone">
<p>Email:<br>
<input type="text" name="Email">
<p>Message:* <br>
<textarea name="Message"></textarea>
<p><input type="submit" name="submit" value="Submit">
</form>
<p>



the php process code is this:
// get posted data into local variables
$EmailFrom = "visitor";
$EmailTo = "someone@yahoo.com";
$Subject = "website inquiry";
$Name = Trim(stripslashes($_POST['Name'])); 
$Telephone = Trim(stripslashes($_POST['Telephone'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$Message = Trim(stripslashes($_POST['Message'])); 

// validation
$validationOK=true;
if (Trim($Name)=="") $validationOK=false;
if (Trim($Message)=="") $validationOK=false;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Telephone: ";
$Body .= $Telephone;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.htm\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>



and i want to integrate this captcha code which i will call ver.php:


<?php
session_start();
$image = imagecreatefrompng('img_ver.png');
$text = sha1(rand(0,9999));
$new_text = substr($text, 17, 6);
$_SESSION['img_ver'] = $new_text;
imagestring($image, 5, 25, 8, $new_text, 0xcccccc);
// $gaussian = array(array(1.0, 2.0, 1.0), array(2.0, 4.0, 2.0), array(1.0, 2.0, 1.0));
//imageconvolution($image, $gaussian, 10, 0);
header('Content-Type: image/png');
imagepng($image, null, 9);
?>



Any input is very much appreciated. Thanks!

This post has been edited by mobius19: 03 August 2009 - 06:24 PM


Is This A Good Question/Topic? 0
  • +

Replies To: integrating captcha on my contact form

#2 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: integrating captcha on my contact form

Posted 03 August 2009 - 09:35 AM

should be simple enough just use

<img src="ver.php" alt="Captcha Image" width="100px" height="100px"/>
<input name="captchatext" type="text" value=""/>


** i did not test your image creation so if it already works that should work **

than at the top of you process be sure to start the session with session_start() and compare right away your variables ...

session_start();
if ($_SESSION['img_ver'] != $_POST['captchatext']) {
   // change this to what you want to do if something goes wrong  
die("not a match for the captcha");
}



you can also have a peak at some snippets online and one here

http://www.dreaminco...snippet3567.htm

This post has been edited by RPGonzo: 03 August 2009 - 09:40 AM

Was This Post Helpful? 0
  • +
  • -

#3 mobius19  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 26-November 08

Re: integrating captcha on my contact form

Posted 03 August 2009 - 06:41 PM

View PostRPGonzo, on 3 Aug, 2009 - 08:35 AM, said:

should be simple enough just use

<img src="ver.php" alt="Captcha Image" width="100px" height="100px"/>
<input name="captchatext" type="text" value=""/>


** i did not test your image creation so if it already works that should work **

than at the top of you process be sure to start the session with session_start() and compare right away your variables ...

session_start();
if ($_SESSION['img_ver'] != $_POST['captchatext']) {
   // change this to what you want to do if something goes wrong  
die("not a match for the captcha");
}



you can also have a peak at some snippets online and one here

http://www.dreaminco...snippet3567.htm



WOW THAT'S GREAT! THANKS. IT WORKED 101%!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1