【发布时间】:2017-03-03 17:37:55
【问题描述】:
有人可以帮我解决这个问题吗?
我正在尝试将此代码添加到我的联系表单以防止垃圾邮件,但它不起作用。
HTML:
Access code: <input type="text" name="code" /><br />
请在上方输入MYCODE。
PHP:
if (strtolower($_POST['code']) != 'mycode') {die('Wrong access code');}
所以问题是由于某种原因,如果代码正确,它不会重定向回感谢页面。
这是我尝试使其工作的方法:
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;
}
这是 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"));
?>
我只需要一种方法来验证人类或阻止机器人,但如果两者都能工作,那就太棒了:)
谢谢。
【问题讨论】:
-
我更改了此代码。请检查这个新代码。
-
你这是个问题。必须改变 { }
-
并添加此 { header('Location: ../thank-you.php'); exit('重定向到../thank-you.php'); }
-
@hasanmovahed 您应该发布答案,而不是尝试编辑问题
-
@hasanmovahed 您能否在答案中分享您的解决方案?我真的无法在 cmets 部分阅读它。提前致谢
标签: php contact-form spam-prevention