在Php联系表单中添加垃圾邮件控制

时间:2017-03-03 17:37:56

标签: php contact-form spam-prevention

有人可以帮我解决这个问题吗?

我正在尝试将此代码添加到我的联系表单中以防止垃圾邮件,但它无效。

HTML:

Access code: <input type="text" name="code" /><br />

请在上面输入 MYCODE

腓:

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"));





?>

我只需要一种方法来验证人体或阻止机器人,但如果两者都能正常工作它会很棒:)

感谢。

1 个答案:

答案 0 :(得分:2)

当你使用if语句时,如果在(...)之后你有多行代码,就像在你的例子中一样,你必须使用大括号。否则只读取第一行代码。因此无论如何都会调用exit。

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;

}

更新

我已经重构/修复了您的代码并在必要时添加了评论。

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;

}

请注意,我删除了您的代码的原始评论