php
  • contact-form
  • spam-prevention
  • 2017-03-03 4 views 0 likes 
    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

    の元のコメントを削除しますのでご注意ください。 plzはこの新しいコードをチェックします。 –

    +0

    これは問題です。 {} –

    +0

    を変更してthis {header( 'Location:../thank-you.php');を追加する必要があります。 exit( 'あなたを../thank-you.phpにリダイレクトします); } –

    答えて

    2

    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

    関連する問題