PHP反垃圾邮件字段

I have a problem with my email form. Everything works fine, except the Anti-Spam field.

The anti-Spam field shows me a wrong answer in all situations. Both if it is true and false, but when i leave the field blank, then the email is send correctly. So this is problem one.

Problem two is: I want when the Anti-Spam answer is correct then a new question not to be generated. I want to remember the question and the answer, when the answer is correct

So look at my code and please help me? what i am doing wrong?

PHP code:

<?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>    

In order to change the values of $user_result inside createCaptcha you need to declare it as global in your function.

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;
     }
}

@Alon

Thanks, I found the solution for problem one:

But second problem still exist.

This is solution for problem one:

<?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;
 }
}

Problem second maybe...

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

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

}