PHP反垃圾邮件字段

时间:2015-11-14 20:41:36

标签: php phpmailer captcha

我的电子邮件表单有问题。一切正常,除了反垃圾邮件领域。

反垃圾邮件字段在所有情况下都显示错误的答案。如果它是真的和假的,但是当我将该字段留空时,则正确发送电子邮件。所以这是问题一。

问题二是:我想在反垃圾邮件答案正确时再生成一个新问题。当答案是正确的时候,我想记住问题和答案

那么看看我的代码,请帮助我?我做错了什么?

PHP代码:

<?php

require './PHPMailer/PHPMailerAutoload.php';

// varijable
$err_name = $err_email = $err_message = $err_forma = $uspesno = $captcha = "";
$name = $email = $message  = $user_result = $arg_1 = $arg_2 ="";


// Konfiguracija PHPMailer-a

$mailer = new PHPMailer;
try {
    if (isset($_POST['submit'])) {

        $name    = isset($_POST['name']) ? $_POST['name'] : FALSE;
        $email   = isset($_POST['email']) ? $_POST['email'] : FALSE;
        $message = isset($_POST['message']) ? $_POST['message'] : FALSE;
          $user_result = isset($_POST['result']) ? $_POST['result'] : FALSE;
          $arg_1 = isset($_POST['arg_one']) ? $_POST['arg_one'] : FALSE;
          $arg_2 = isset($_POST['arg_two']) ? $_POST['arg_two'] : FALSE;


        $mailer->From     = $email; // Email posaljioca
        $mailer->FromName = "Nova Porudzbina"; // Ime Posaljioca
        $mailer->AddAddress("blabla@gmail.com"); //adresa na koju se salje
        $mailer->isHTML(TRUE); // set email format to HTML
        $mailer->WordWrap = 50; // set word wrap to 50 characters
        $mailer->CharSet  = "utf-8"; //"ukljucuje" cirlicna slova, kao i latinicna sa kvacicama


        $mailer->Subject = 'zahtev za podršku: ' . $naziv_servera;


        if ($_SERVER["REQUEST_METHOD"] == "POST") {

            $name     = test_input($_POST["name"]);
            $name_exp = "/^[A-Za-z\p{L} .'-]{2,40}+$/u"; // Dozvoljava naša slova i ograničava da najmanje može 2 a najviše 40 karaktera

            if (!preg_match($name_exp, $name)) {
                $err_name .= 'Vaše ime nije validno.';


            }


            $email     = test_input($_POST["email"]);
            $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

            if (!preg_match($email_exp, $email)) {

                $err_email .= 'Vaša e-mail adresa nije validna.';

            }

            $message     = test_input($_POST["message"]);
            $message_exp = "/^[A-Za-z\p{L} .'-]{2,400}+$/u";

            if (!preg_match($message_exp, $message)) {

                $err_message .= 'Vaša poruka nije validna.';
          }

          $user_result     = test_input($_POST["result"]);

          if($total <> $user_result) {
          $captcha .= 'Anti-spam odgovor koji ste uneli nije tačan.';
          }
        }


        // Body

        $body = "<h2 style='background: red; color: #fff;'>Nova Porudzbina</h2>";
        $body .= "<b>Ime i Prezime:</b>" . $name . "<br>";
        $body .= "<b>Email:</b>" . $email . "<br>";
        $body .= "<b>Poruka:</b>" . $message . "<br>";

        $mailer->Body = $body;

        // Posalji
          if (strlen($err_name == "" && $err_email == "" && $err_message == "" && $total == $user_result)) {
            $mailer->send(); // ako nema nikakve greške - pošalji e-mail
            $uspesno .= 'Vasa poruka je poslata';
        }
    }
}

catch (phpmailerException $ex) {
    echo $ex->errorMessage();
}
catch (Exception $ex) {
    echo $ex->getMessage();
}

function test_input($data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

function generateFieldNumber($min = 1, $max = 3)
{
     return rand(1, 3);
}

function createCaptcha($arg_1 = '', $arg_2 = '', $total = 0)
{


     if(isset($_POST['submit'])) {

          $arg_1 = $_POST['arg_one'];
          $arg_2 = $_POST['arg_two'];
          $user_result = $_POST['result'];

          $total = $arg_1 + $arg_2;


     }
}

HTML code:

<?php
include "send_email.php";
?>

<?php createCaptcha(); ?>

<span><?php echo $uspesno;?></span>
         <form  action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="action" value="submit">
    <div class="name">Name:</div>
    <input name="name" type="text" value="<?php echo $name;?>" size="30"/>
    <span><?php echo $err_name;?></span>
    <div class="email">Email:</div>
    <input name="email" type="text" value="<?php echo $email;?>" size="30"/>
    <span><?php echo $err_email;?></span>
    <div class="message">Message:</div>
    <textarea name="message" rows="7" cols="30"><?php echo $message;?></textarea><br>
    <span><?php echo $err_message;?></span><br><br>
     <label>Anti-Spam:</label>
     <input type="text" name="arg_one" value="<?php echo generateFieldNumber();?>"   size="2">
     + <input type="text" name="arg_two" value="<?php echo generateFieldNumber();?>"  size="2">
     = <input type="text" name="result" value="<?php echo $user_result;?>"  size="2">
     <span><?php echo $captcha;?></span><br>
        <input type="submit" name="submit" value="Submit" id="submit">
    </form>    

2 个答案:

答案 0 :(得分:0)

要更改 createCaptcha function createCaptcha() { global $user_result, $arg_1, $arg_2, $total; if(isset($_POST['submit'])) { $arg_1 = $_POST['arg_one']; $arg_2 = $_POST['arg_two']; $user_result = $_POST['result']; $total = $arg_1 + $arg_2; } } 的值,您需要在函数中将其声明为全局值。

fullPath

答案 1 :(得分:0)

@Alon

谢谢,我找到了第一个问题的解决方案:

但第二个问题仍然存在。

这是问题一的解决方案:

<?php

require './PHPMailer/PHPMailerAutoload.php';

// varijable
$err_name = $err_email = $err_message = $err_forma = $uspesno = $captcha =  "";
$name = $email = $message  = $user_result  = $arg_1 = $arg_2 = "";


// Konfiguracija PHPMailer-a

$mailer = new PHPMailer;
try {
    if (isset($_POST['submit'])) {

        $name    = isset($_POST['name']) ? $_POST['name'] : FALSE;
        $email   = isset($_POST['email']) ? $_POST['email'] : FALSE;
        $message = isset($_POST['message']) ? $_POST['message'] : FALSE;
          $user_result = isset($_POST['result']) ? $_POST['result'] : FALSE;
          $arg_1 = isset($_POST['arg_one']) ? $_POST['arg_one'] : FALSE;
          $arg_2 = isset($_POST['arg_two']) ? $_POST['arg_two'] : FALSE;
          $total = $arg_1 + $arg_2;

        $mailer->From     = $email; // Email posaljioca
        $mailer->FromName = "Nova Porudzbina"; // Ime Posaljioca
        $mailer->AddAddress("blabla@gmail.com"); //adresa na koju se salje
        $mailer->isHTML(TRUE); // set email format to HTML
        $mailer->WordWrap = 50; // set word wrap to 50 characters
        $mailer->CharSet  = "utf-8"; //"ukljucuje" cirlicna slova, kao i latinicna sa kvacicama


        $mailer->Subject = 'zahtev za podršku: ' . $naziv_servera;


        if ($_SERVER["REQUEST_METHOD"] == "POST") {

            $name     = test_input($_POST["name"]);
            $name_exp = "/^[A-Za-z\p{L} .'-]{2,40}+$/u"; // Dozvoljava naša slova i ograničava da najmanje može 2 a najviše 40 karaktera

            if (!preg_match($name_exp, $name)) {
                $err_name .= 'Vaše ime nije validno.';


            }


            $email     = test_input($_POST["email"]);
            $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

            if (!preg_match($email_exp, $email)) {

                $err_email .= 'Vaša e-mail adresa nije validna.';

            }

            $message     = test_input($_POST["message"]);
            $message_exp = "/^[A-Za-z\p{L} .'-]{2,400}+$/u";

            if (!preg_match($message_exp, $message)) {

                $err_message .= 'Vaša poruka nije validna.';
          }

          $user_result     = test_input($_POST["result"]);

          if($total <> $user_result) {
          $captcha .= 'Anti-spam odgovor koji ste uneli nije tačan.';
          }
        }


        // Body

        $body = "<h2 style='background: red; color: #fff;'>Nova Porudzbina</h2>";
        $body .= "<b>Ime i Prezime:</b>" . $name . "<br>";
        $body .= "<b>Email:</b>" . $email . "<br>";
        $body .= "<b>Poruka:</b>" . $message . "<br>";

        $mailer->Body = $body;

        // Posalji
          if (strlen($err_name == "" && $err_email == "" && $err_message == "" && $captcha == "")) {
            $mailer->send(); // ako nema nikakve greške - pošalji e-mail
            $uspesno .= 'Vasa poruka je poslata';
        }
    }
}

catch (phpmailerException $ex) {
    echo $ex->errorMessage();
}
catch (Exception $ex) {
    echo $ex->getMessage();
}

function test_input($data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

function generateFieldNumber($min = 1, $max = 3)
{
     return rand(1, 3);
}

function createCaptcha() {
    global $user_result, $arg_1, $arg_2, $total;

 if(isset($_POST['submit'])) {

      $arg_1 = $_POST['arg_one'];
      $arg_2 = $_POST['arg_two'];
      $user_result = $_POST['result'];

      $total = $arg_1 + $arg_2;
 }
}

第二个问题可能......

function generateFieldNumber($min = 1, $max = 3)
{
     return rand(1, 3);
    if($captcha == "")

 /*  than remember or stop generate new number ? */

}