Forum Moderators: coopster

Message Too Old, No Replies

PHP Captcha

         

Geoffrey james

12:31 am on Jan 7, 2007 (gmt 0)

10+ Year Member



Hi all, happy new year too.

I want to put a PHP Captcha on a form of mine but as ive always sent sent them via CGI im unsure of what to do.

my usual postings are as such:

<form id="...." action="http://www.--------.com/cgi-bin/contact.cgi" method="post">

I have noticed with the PHP they want to have action as .php

Unsure what to do please help.

Geoff Boult

barns101

2:35 pm on Jan 7, 2007 (gmt 0)

10+ Year Member



You can either get a ready made script by Googling php contact form [google.com] or write one yourself using the PHP mail() [php.net] function.

scriptmasterdel

3:21 pm on Jan 7, 2007 (gmt 0)

10+ Year Member



Hi Geoffrey_james and a happy new year to you,

If you need assistanse with capcha then i have always found that the "pureMango freeCap" capcha proof script is more then enough to protect any form.

Regarding the sending of e-mails the mail() function is the way to go but i would suggest that you impliment some kind of injection trap to protect your form against spammers.

You can find out more about this here: [webmasterworld.com...]

Good luck,

Del

Geoffrey james

12:12 am on Jan 8, 2007 (gmt 0)

10+ Year Member



Thanks all, having slight trouble though, not recieving the form although it says its been sent.

Please look over my details:

I have a contact.html page with form on like so----

<!-- start contact form -->
<form id="contact" action="mailer.php" method="post">
<fieldset>
<legend id="contact">PERSONAL</legend>
<label for="firstname">Name : </label>
<input type="text" name="name" />
<br />
<label for="subject">Subject : </label>
<input type="text" name="subject" />
<br />
<label for="email">Email : </label>
<input type="text" name="email"/>
</fieldset>

<fieldset>
<legend id="contact">ENQUIRY</legend>
<label for="comments">Comments : </label>
<textarea rows="10" name="message" cols="57"/>
</textarea>
<br />
</fieldset>

<input type="submit" name="submit" value="Submit" />
</form>
<!-- end contact form -->


and a page as mailer.php-----

<?php
if(isset($_POST['submit'])) {

$to = "enquiries@MY_SITE.com";
$subject = "Enquiry Form";
$name_field = $_POST['name'];
$subject_field = $_POST['subject'];
$email_field = $_POST['email'];
$message = $_POST['message'];

$body = "From: $name_field\n E-Mail: $email_field\n Subject:\n $subject_field\n Message:\n $message";

echo "Data has been submitted to $to!";
mail($to, $subject, $body);

} else {

echo "ERROR, no data was sent!";

}
?>

both have been uploaded but have checked my email and nothing arrived.

please help. Script from: Kirupa

cheers Geoff

scriptmasterdel

12:24 am on Jan 8, 2007 (gmt 0)

10+ Year Member



What might be a good suggestion is you should validate if the e-mail has been sent or not for example:

if (mail($to, $subject, $body)
{
// it's gone
}
else
{
// it has failed.
}

Also, i am not sure if you will need to add some headers, so when a message is received the e-mail client has a name of the sender.

e.g.
$headers = "From: Your name <you@site.com>\r\n";
mail($to, $subject, $body, $headers);

I hope i have helped.

Del

Geoffrey james

12:42 am on Jan 8, 2007 (gmt 0)

10+ Year Member



Hi Del...happy new year by the way.

Thanks for all your help, it has worked now and I have two messages in my inbox from me. I must stress that im real new to PHP (reading sitepoint basic php MySQL now) anyway, I do need to validate though as one of the forms i sent i did not fill any feilds in and it still went.

can you point out where you would place these on the form coding i presented last message. (Sorry to be a burden)

one other thing I would like to do and thats to send thank you back via a thankyou.html page that is created but just needs connecting up. can you point me in the right direction please.

cheers for all help-geoff

dreamcatcher

11:36 am on Jan 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if (mail($to, $subject, $body)

This doesn`t actually state whether the e-mail has been sent or not, it simply returns true if the statement has been exectuted or not.

dc

scriptmasterdel

12:04 pm on Jan 8, 2007 (gmt 0)

10+ Year Member



Thanks dreamcatcher,

I should really read what i have written beforei send the post, thank you for correcting me!

Del

supermanjnk

3:22 am on Jan 9, 2007 (gmt 0)

10+ Year Member




if (mail($to, $subject, $body)

This doesn`t actually state whether the e-mail has been sent or not, it simply returns true if the statement has been exectuted or not.

dc

Actually, mail() only returns true if it's accepted for delivery (which 99% of the time would mean it was sent), otherwise it will return false. though the code scriptmasterdel still wouldn't work, but thats because he's missing a closing )

From PHP.net


Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.

If you haven't received the email, have you checked the spam filter? Without headers there is a very very high chance it was either put in a spam filter, or if you have a corporate filter or if your email host has filters, deleted.

Geoffrey james

6:17 am on Jan 9, 2007 (gmt 0)

10+ Year Member



Hi guys, still having trouble, see what you think.

I would like a bit of help if you could regarding PHP captcha's.

Im stuck. No image is coming up on form. here is my page situation.

1 contact.html page with the following form on it. Links to mailer.php page for sending form, code as follows:

<!-- start contact form -->
<form id="contact" action="mailer.php" method="post">
<fieldset>
<legend id="contact">PERSONAL</legend>
<label for="firstname">Name : </label>
<input type="text" name="name" size="32" />
<br />
<label for="subject">Subject : </label>
<input type="text" name="subject" size="32" />
<br />
<label for="email">Email : </label>
<input type="text" name="email" size="32" />
</fieldset>
<fieldset>
<legend id="contact">ENQUIRY</legend>
<label for="comments">Comments : </label>
<textarea rows="10" name="message" cols="57"/>
</textarea>
</fieldset>
<fieldset>
<legend id="contact">SECURITY</legend>
<img src="CaptchaSecurityImages.php" />

<input id="security_code" name="security_code" type="text" />
</fieldset>
<input type="submit" name="submit" value="Submit" size="10"/>
</form>
<!-- end contact form -->

----------------------------------------------------------------------

then I have a file: CaptchaSecurityImages.php which contains the following code:

<?php
session_start();


class CaptchaSecurityImages {

var $font = 'verdana';

function generateCode($characters) {
/* list all possible characters, similar looking characters and vowels have been removed */
$possible = '23456789bcdfghjkmnpqrstvwxyz';
$code = '';
$i = 0;
while ($i < $characters) {
$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
$i++;
}
return $code;
}

function CaptchaSecurityImages($width='120',$height='40',$characters='6') {
$code = $this->generateCode($characters);
/* font size will be 75% of the image height */
$font_size = $height * 0.75;
$image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
/* set the colours */
$background_color = imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 20, 40, 100);
$noise_color = imagecolorallocate($image, 100, 120, 180);
/* generate random dots in background */
for( $i=0; $i<($width*$height)/3; $i++ ) {
imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
}
/* generate random lines in background */
for( $i=0; $i<($width*$height)/150; $i++ ) {
imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
}
/* create textbox and add text */
$textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
$x = ($width - $textbox[4])/2;
$y = ($height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
/* output captcha image to browser */
imagejpeg($image);
imagedestroy($image);
$_SESSION['security_code'] = $code;
}

}

$width = isset($_GET['width'])? $_GET['width'] : '120';
$height = isset($_GET['height'])? $_GET['height'] : '40';
$characters = isset($_GET['characters'])? $_GET['characters'] : '6';

header('Content-Type: image/jpeg');
$captcha = new CaptchaSecurityImages($width,$height,$characters);

?>

----------------------------------------------------------------------

I also have file: mailer.php which is probably causing the problems, here it is:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Contact Form</title>
</head>

<body>

<?php
if(isset($_POST['submit'])) {

$to = "enquiries@MY_SITE.com";
$subject = "Enquiry Form";
$name_field = $_POST['name'];
$subject_field = $_POST['subject'];
$email_field = $_POST['email'];
$message = $_POST['message'];

$body = "From: $name_field\n E-Mail: $email_field\n Subject:\n $subject_field\n Message:\n $message";

echo "Data has been submitted to $to!";
mail($to, $subject, $body);

} else {

echo "ERROR, no data was sent!";

}

if(($_SESSION['security_code'] == $_POST['security_code']) && (!empty($_SESSION['security_code'])) ) {
// Thank you, your message has been sent.
} else {
// Sorry, wrong security code entered.
}
?>

</body>
</html>

----------------------------------------------------------------------

please can anyone help.

or give better suggestions, dont really know PHP at all.

Cheers Geoff

[edited by: jatar_k at 6:59 am (utc) on Jan. 9, 2007]