ndowens Posted March 30, 2010 Share Posted March 30, 2010 <?php session_start(); class contactme { function contact() { $to = 'email@email.com'; $from = $_POST['from']; $subject = $_POST['subject']; $message = $_POST['message']; if($from && $subject && $message) { mail($to, "$subject", $message, "From:$from"); echo "Thank you, $from, message has been sent"; } else { echo "All fields are required in order to send message"; } } } class captcha { function _captcha() { $captchamsg = rand(500,7000); $_SESSION['saved'] = $captchamsg; $captchainput = $_POST['captcha']; echo "$captchamsg <br />"; if(empty($_POST['captcha'])) { echo ""; } if(!empty($_POST['captcha']) && $_SESSION['saved'] == $_POST['captcha']) { $post = new contactme(); $post->contact(); session_destroy(); } else { echo "Sorry CAPTCHA didn't match"; session_destroy(); } } } ?> <form method="post" action="<?php $_SERVER['PHP_SELF'];?>"> Email:<br /> <input type="text" name="from"><br /> Subject:<br /> <input type="text" name="subject"><br /> Message:<br /> <textarea name="message"></textarea> <br /> Captcha: <?php $captcha = new captcha(); $captcha->_captcha(); echo "<br /><input type='text' name='captcha'><br />"; echo "<br />"; echo <<<BODY <br /> <input type="submit" value="Submit Message"> </form> BODY; echo "<br />"; ?> I can not get it to work, from what it appears is that the browser isn't remembering what the answer to the math problem is and what was input'd from the user. I have rewrote this script over and over and it just doesn't want to work. Quote Link to comment https://forums.phpfreaks.com/topic/196956-php-mail-and-captcha/ Share on other sites More sharing options...
the182guy Posted March 30, 2010 Share Posted March 30, 2010 It didn't work because you were overriting the captcha in the session before the script checked for a match. Also, you were destroying the session if the captcha did not match which would stop any further attempts. I have moved the captcha generation lines to after the check, and I've removed the session_destroy if the captcha did not match. Here's the working script. I've also added indentation so the code is easier to read. <?php session_start(); class contactme { function contact() { $to = 'email@email.com'; $from = $_POST['from']; $subject = $_POST['subject']; $message = $_POST['message']; if($from && $subject && $message) { mail($to, "$subject", $message, "From:$from"); echo "Thank you, $from, message has been sent"; } else { echo "All fields are required in order to send message"; } } } class captcha { function _captcha() { $captchainput = $_POST['captcha']; if(empty($_POST['captcha'])) { echo ""; } if(!empty($_POST['captcha']) && $_SESSION['saved'] == $_POST['captcha']) { $post = new contactme(); $post->contact(); session_destroy(); } else { echo "Sorry CAPTCHA didn't match"; //session_destroy(); } $captchamsg = rand(500,7000); $_SESSION['saved'] = $captchamsg; echo "$captchamsg <br />"; } } ?> <form method="post" action="<?php $_SERVER['PHP_SELF'];?>"> Email:<br /> <input type="text" name="from"><br /> Subject:<br /> <input type="text" name="subject"><br /> Message:<br /> <textarea name="message"></textarea> <br /> Captcha: <?php $captcha = new captcha(); $captcha->_captcha(); echo "<br /><input type='text' name='captcha'><br />"; echo "<br />"; echo <<<BODY <br /> <input type="submit" value="Submit Message"> </form> BODY; echo "<br />"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/196956-php-mail-and-captcha/#findComment-1033995 Share on other sites More sharing options...
ndowens Posted March 30, 2010 Author Share Posted March 30, 2010 I even copied the code you provided, and it still doesn't work, it still doesn't seem to remember the answer and what was typed for the captcha. Quote Link to comment https://forums.phpfreaks.com/topic/196956-php-mail-and-captcha/#findComment-1034281 Share on other sites More sharing options...
ndowens Posted March 30, 2010 Author Share Posted March 30, 2010 As well I tried starting from scratch and nothing <?php class contactme { function contact() { $to = '****'; $_SESSION[input] = $_POST['captcha']; $from = $_POST['from']; $subject = $_POST['subject']; $message = $_POST['message']; $captcha = $_POST['captcha']; if($from && $subject && $message) { mail($to, "$subject", $message, "From:$from"); echo "Thank you, $from, message has been sent"; } else { echo "All fields are required in order to send message"; } } } class captcha { function intCap() { session_start(); $math[1] = rand(0,20); $math[2] = rand(0,20); $_SESSION[ans] = $math[1] + $math[2]; echo "$math[1]".'+'."$math[2]"; $_SESSION[input] = $_POST['captcha']; if(!empty($_POST['captcha']) && $_SESSION[ans] == $_SESSION[input]) { echo "They match"; session_destroy(); } else { echo ""; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/196956-php-mail-and-captcha/#findComment-1034346 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.