Jump to content

PHP mail and Captcha


ndowens

Recommended Posts

<?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.

Link to comment
Share on other sites

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 />";
?>

Link to comment
Share on other sites

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 "";
   }
    
  }
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.