I'm using this working Contact Form script code:
<?php
$data = json_decode(file_get_contents("php://input"));
$name = trim($data->name);
$name = str_replace(array("\r", "\n"), array(" ", " "), $name);
$email = filter_var(trim($data->email), FILTER_SANITIZE_EMAIL);
$message = trim($data->message);
// Check that data was sent.
if (empty($name) || empty($message) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "One or more invalid entries. Please try again.";
exit;
}
$to = "support@...com";
$from = "From: contact-form@...com". "\r\n";
$body = "A message has been sent via the website contact form.\n\n";
$body .= "Name: $name\n";
$body .= "Email: $email\n\n";
$body .= "Message:\n$message\n";
if (mail($to, 'Customer Inquiry', $body)){
echo "Thank You. Your Message Has Been Sent.";
} else {
echo "An error has occurred and your message could not be sent.";
}
?>
And I'm trying to integrate this captcha code into it:
<?php
session_start();
if(isset($_POST["captcha"])&&$_POST["captcha"]!=""&&$_SESSION["code"]==$_POST["captcha"])
{
echo "Correct Code Entered";
//Do your stuff
}
else
{
die("Wrong Code Entered");
}
?>
like this:
<?php
[B][COLOR="#FF8C00"]session_start();[/COLOR][/B]
$data = json_decode(file_get_contents("php://input"));
$name = trim($data->name);
$name = str_replace(array("\r", "\n"), array(" ", " "), $name);
$email = filter_var(trim($data->email), FILTER_SANITIZE_EMAIL);
$message = trim($data->message);
// Check that data was sent.
if (empty($name) || empty($message) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "One or more invalid entries. Please try again.";
exit;
}
[B][COLOR="#FF8C00"]if(isset($_POST["captcha"])&&$_POST["captcha"]!=""&&$_SESSION["code"]==$_POST["captcha"])
{
echo "Correct Code Entered";
//Do your stuff
}
else
{
die("Wrong Code Entered");
}[/COLOR][/B]
$to = "support@...com";
$from = "From: contact-form@...com". "\r\n";
$body = "A message has been sent via the website contact form.\n\n";
$body .= "Name: $name\n";
$body .= "Email: $email\n\n";
$body .= "Message:\n$message\n";
if (mail($to, 'Customer Inquiry', $body)){
echo "Thank You. Your Message Has Been Sent.";
} else {
echo "An error has occurred and your message could not be sent.";
}
?>
but after I tested/completed the Form, including entering the correct Captcha code, I see the message "Wrong Code Entered", and of course the Contact Form info does not send.
Then I added this (after the 'session start' line):
var_dump($_SESSION);
and ran the Form, and I see this:
array(2) { ["security_code"]=> string(6) "9569qb" ["code"]=> int(6133) } Wrong Code Entered
Any guidance/insight about integrating the captcha script successfuly will be appreciated.