Can somebody please help me to solve this?
I am trying to add this code to my contact form to prevent spam emails but it is not working.
Html:
Access code: <input type="text" name="code" /><br />
Please enter MYCODE above.
Php:
if (strtolower($_POST['code']) != 'mycode') {die('Wrong access code');}
so problem is for some reason it is not redirecting back to Thank you page if the code is correct.
Here is how I tried to make it work:
if (strtolower($_POST['code']) == 'mycode')
header( 'Location: ../thank-you.php' );
exit('Redirecting you to ../thank-you.php');
if (strtolower($_POST['code']) != 'mycode')
{
die('Wrong access code! Please go back and try again.');
exit;
}
Here is the FULL CODE for PHP :
<?php
require_once('class.phpmailer.php');
include("class.smtp.php");
$myaddress = "my@emailaddress.com";
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$lastname = $_POST['lastname'];
$bizphone = $_POST['bizphone'];
$state = $_POST['state'];
$phone = $_POST['phone'];
$comments = $_POST['comments'];
$code = $_POST['code'];
//This line is checking the input named code to verify humans
if (strtolower($_POST['code']) == 'mycode') {
header( 'Location: ../thank-you.php' );
exit('Redirecting you to ../thank-you.php');
}
if (strtolower($_POST['code']) != 'mycode')
{
die('Wrong access code! Please go back and try again.');
exit;
}
// This code checks the hidden fields only bots can fill to verify humans
if(strlen($lastname) !== 0)
{
header( 'Location: ../thank-you.php' );
exit;
}
if(strlen($bizphone) !== 0)
{
header( 'Location: ../thank-you.php' );
exit;
}
$ip = $_POST['ip'];
$httpref = $_POST['httpref'];
$httpagent = $_POST['httpagent'];
$mailst = $_POST['mailst'];
$emailbody = "
<p>You have received a Quote !</p><br />
<p><strong>First - Last Name:</strong> {$name} </p>
<p><strong>Email Address:</strong> {$email} </p>
<p><strong>Telephone:</strong> {$phone} </p>
<p><strong>Additional Comments:</strong> {$comments}</p>
<p><strong>Ip Address:</strong> {$ip}</p>
<p><strong>Refererer:</strong> {$httpref}</p>
<p><strong>User Agent:</strong> {$httpagent}</p>
";
class myphpmailer extends PHPMailer
{
// Set default variables for all new objects
public $From = "";
public $FromName = "";
public $Sender = "";
//public $Subject = $quoterequest;
public $Host = '';
public $Port = ;
public $SMTPSecure = 'ssl';
public $SMTPAuth = true;
public $Username = '';
public $Password = '';
}
$mail = new myphpmailer;
#!!!!!CHANGE SMTPDebug level for debugging!!!
$mail->SMTPDebug = 0;
$mail->Subject = "Contact Form";
$mail->IsSMTP();
$mail->AddAddress($myaddress);
$mail->MsgHTML($emailbody);
$mail->SMTPAuth = true;
$mail->Send();
exit(header("Location: ../thankyou.php"));
?>
I only need one method to verify humans or block bots but if both will work it will be awesome :)
Thanks.
When you use if statements, and you have more than one line of code after callling if(...), like in your example, you must use braces. Otherwise only the first line of code is read. So exit will be called no matter what.
if (strtolower($_POST['code']) == 'mycode') {
header( 'Location: ../thank-you.php' );
exit('Redirecting you to ../thank-you.php');
}
if (strtolower($_POST['code']) != 'mycode')
{
die('Wrong access code! Please go back and try again.');
exit;
}
UPDATE
I have refactored/fixed your code and added a comment where necessary.
require_once('class.phpmailer.php');
include("class.smtp.php");
// Validate fields
if (!isset($_POST['lastname'])) {
die('Wrong last name...');
}
if (!isset($_POST['bizphone'])) {
die('Wrong bizphone...');
}
// add other validation here
if (strtolower($_POST['code']) != 'mycode') {
die('Wrong access code! Please go back and try again.');
}
// initiate variables after validation
$myaddress = "my@emailaddress.com";
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$lastname = $_POST['lastname'];
$bizphone = $_POST['bizphone'];
$state = $_POST['state'];
$phone = $_POST['phone'];
$comments = $_POST['comments'];
$code = $_POST['code'];
$ip = $_POST['ip'];
$httpref = $_POST['httpref'];
$httpagent = $_POST['httpagent'];
$mailst = $_POST['mailst'];
$emailbody = "<p>You have received a Quote !</p><br />
<p><strong>First - Last Name:</strong> {$name} </p>
<p><strong>Email Address:</strong> {$email} </p>
<p><strong>Telephone:</strong> {$phone} </p>
<p><strong>Additional Comments:</strong> {$comments}</p>
<p><strong>Ip Address:</strong> {$ip}</p>
<p><strong>Refererer:</strong> {$httpref}</p>
<p><strong>User Agent:</strong> {$httpagent}</p>
";
class myphpmailer extends PHPMailer
{
public $From = "";
public $FromName = "";
public $Sender = "";
public $Host = '';
public $Port = ''; // <-- You had a syntax error here, missing the semicolons
public $SMTPSecure = 'ssl';
public $SMTPAuth = true;
public $Username = '';
public $Password = '';
}
// send mail only if code is correct
if (strtolower($code) == 'mycode') {
$mail = new myphpmailer;
$mail->SMTPDebug = 0;
$mail->Subject = "Contact Form";
$mail->IsSMTP();
$mail->AddAddress($myaddress);
$mail->MsgHTML($emailbody);
$mail->SMTPAuth = true;
/**
* WHERE ARE YOUR CREDENTIALS
*/
$mail->Host = "mail.yourdomain.com";
$mail->Port = 25;
$mail->Username = "yourname@yourdomain.com";
$mail->Password = "yourpassword";
$mail->Send();
header('Location: ../thank-you.php');
exit;
}
Please note that i removed original comments of your code