apg1985 Posted January 14, 2009 Share Posted January 14, 2009 Hi Guys, new to php so have a basic understanding. I've got the email sending and a redirect to the homepage. But I want a captcha for security. Ive got the image for the captcha displaying but not sure how I would make it work. html captcha: <img src="aaaacaptcha.php" style="display: block;margin-bottom: 5px;"/> <input type="text" name="captcha2" id="captcha2" value="captcha2" /> code for the captcha image: <?php session_start(); $RandomStr = md5(microtime()); $ResultStr = substr($RandomStr,0,5); $NewImage =imagecreatefromjpeg("img.jpg"); $LineColor = imagecolorallocate($NewImage,233,239,239); $TextColor = imagecolorallocate($NewImage, 255, 255, 255); imageline($NewImage,1,1,40,40,$LineColor); imageline($NewImage,1,100,60,0,$LineColor); imagestring($NewImage, 5, 20, 10, $ResultStr, $TextColor); $_SESSION['captcha'] = $ResultStr; header("Content-type: image/jpeg"); imagejpeg($NewImage); ?> code for the email php: <?php if(isset($_POST['submit'])) { $to = "my email address"; $subject = "web form"; $firstname = $_POST['first name']; $lastname = $_POST['last name']; $body = " email fields"; header("Location: homepage address"); mail($to, $subject, $body); } else { echo "Not Sent"; } ?> Please help Link to comment https://forums.phpfreaks.com/topic/140826-email-form-with-captcha-help-please/ Share on other sites More sharing options...
rhodesa Posted January 14, 2009 Share Posted January 14, 2009 <?php session_start(); if(isset($_POST['submit'])) { if(empty($_POST['captcha2']) || $_SESSION['captcha'] != $_POST['captcha2'] ){ die("Invalid captcha"); } $to = "my email address"; $subject = "web form"; $firstname = $_POST['first name']; $lastname = $_POST['last name']; $body = " email fields"; header("Location: homepage address"); mail($to, $subject, $body); } else { echo "Not Sent"; } ?> Link to comment https://forums.phpfreaks.com/topic/140826-email-form-with-captcha-help-please/#findComment-737075 Share on other sites More sharing options...
apg1985 Posted January 15, 2009 Author Share Posted January 15, 2009 Brilliant that worked, thanks for that. At the moment if its correct it goes through to the homepage which is great, but ive got a error page as well error.php. How would I get it to go to error.php if it was incorrect?? would it be if(isset($_POST['submit'])) { if(empty($_POST['captcha2']) || $_SESSION['captcha'] != $_POST['captcha2'] ){ die("Location: error.php"); } Link to comment https://forums.phpfreaks.com/topic/140826-email-form-with-captcha-help-please/#findComment-737616 Share on other sites More sharing options...
rhodesa Posted January 15, 2009 Share Posted January 15, 2009 header("Location: error.php"); exit; Link to comment https://forums.phpfreaks.com/topic/140826-email-form-with-captcha-help-please/#findComment-737638 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.