簡體   English   中英

在Php聯系人表單中添加垃圾郵件控件

[英]Adding Spam Control in the Php Contact Form

有人可以幫我解決這個問題嗎?

我正在嘗試將此代碼添加到我的聯系表單中,以防止發送垃圾郵件,但它不起作用。

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





?>

我只需要一種方法來驗證人員或阻止機器人,但如果兩種方法都可行,那就太棒了:)

謝謝。

當您使用if語句,並且在調用if(...)之后有多行代碼時(如您的示例),必須使用花括號。 否則,僅讀取第一行代碼。 因此,無論如何都將被稱為出口。

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;

}

請注意,我刪除了您的代碼的原始注釋

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM