2017-03-03 82 views
0

有人可以幫我解決這個問題嗎?在Php聯繫表中添加垃圾郵件控制

我想將此代碼添加到我的聯繫表單,以防止垃圾郵件,但它不起作用。

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 = "[email protected]"; 
     $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")); 





?> 

我只需要一個方法來驗證人類或塊的機器人,但如果兩個將工作它將是真棒:)

謝謝。

+0

的原始意見,我改變了這種代碼。請檢查這個新的代碼。 –

+0

你是這個問題。必須更改{} –

+0

並添加此{header('Location:../thank-you.php'); exit('將您重定向到../thank-you.php'); } –

回答

2

如果使用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; 

} 

UPDATE

我已經重構/固定的代碼,並在必要時添加註釋。

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 = "[email protected]"; 
$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 = "[email protected]"; 
    $mail->Password = "yourpassword"; 


    $mail->Send(); 


    header('Location: ../thank-you.php'); 
    exit; 

} 

請注意,我刪除了你的代碼

+0

感謝您的答案,但這次它甚至沒有發送電子郵件:( – George1995