0

有人可以帮助我并引导我走上正确的道路

我有一个简单的表单和验证,但我的电子邮件中总是收到垃圾邮件,然后我决定将 CAPTCHA 放入其中以阻止机器人发送垃圾邮件。

问题是即使验证码是空白的消息仍然发送到我的电子邮件,我很难指出问题是什么,因为我还在 PHP 的学习过程中简而言之我只有 PHP 的基本知识

顺便说一句,这是我的代码:

HTML

<form method="post" action="#">
    <div class="inputFields">
    <!-- <legend>Comment Form</legend> -->
        <p>
            <label for="name">Name</label><br />
            <input type="text" name="name" id="name"  <?php if(isset($name)){ echo 'value="' . $name . '" />';} else { echo 'value="" />';} ?>
        </p>
        <p>
            <label for="email">Email </label><br />
            <input type="text" name="email" id="email" <?php if(isset($email)){ echo 'value="' . $email . '" />';} else { echo 'value="" />';} ?>
        </p>

        <p>
            <label for="location">Location</label><br />
            <input type="text" name="location" id="location" <?php if(isset($location)){ echo 'value="' . $location . '" />';} else { echo 'value="" />';} ?>
        </p>

        <p>
            <label class="yourMessage" for="comment">Your message</label><br />
            <textarea cols="30" rows="10" name="message" id="comment"><?php if(isset($message)){ echo $message;}?></textarea>
        </p>


        <p>
            <label class="captcha" for="captcha">Enter text from image</label><br />
            <img src="test/seccode.php" width="100" height="36" alt="" /><br /><br />
            <input type="text" name="captcha" value="" id="captcha"/>
        </p>



    </div>
    <p>
        <input class="submit" type="submit" name="submit" value="Submit" />
    </p>
</form>

验证

<?php
$submit = htmlentities($_POST['submit']);
$name = htmlentities($_POST['name']);
$email = htmlentities($_POST['email']);
$message = htmlentities($_POST['message']);

if($submit == 'Submit') {

    ###### Validate 'name' and combat Magic Quotes if necessary

    if(isset($name) && !empty($name)) {
        $name2 = stripslashes($name);
        $nameCheck = strlen($name2);
    } else {
        $err_name = '<span>Please enter your <strong>name</strong>.</span>';
        $isError=true;
    }

    ### check if data has numbers

    if(is_numeric($name) ) {
        $err_name = '<span>Please enter a valid <strong>name</strong>. No numbers please.</span>';
        $isError=true;
    }

    if($_POST['captcha'] != $_SESSION['secCode']) {
        // wrong security code
        $err_captcha = '<span><strong>WRONG CODE!</strong></span>';
        $isError=true;
    }
    else {
        // security code is valid; reset it!
        $_SESSION['secCode'] = rand(100000, 999999);
    }


    ###### Validate 'email' and combat Magic Quotes if necessary

    if(!empty($email) && eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email'])) {
    } else {
        $err_email = '<span>Please enter a valid <strong>email address</strong>.</span>';
        $isError=true;
    }

    ###### Validate 'message' and combat Magic Quotes if necessary

    if(!empty($message)) {
        $message2 = stripslashes($message);
        $messageCheck = strlen($message2);
    } else {
        $err_message = '<span>Please enter your <strong>message</strong>.</span>';
        $isError=true;
    }


    ###### Mailing Address

    $to  = 'myemail@gmail.com';

    $headers="From: $email\n";

    ###### Body of the email to be sent:

    $message1="Name: $name\nEmail: $email\n\nMessage: $message";

    //$message2="$name \'s message:\n\n\"$message\"";


    ###### Mailing the data

    //!!!!!!!!!!! UNCOMMENT MAIL BELOW WHEN UPLOADED TO WEB HOST !!!!!!!!!!!

    if(!isset($isError)) {

        if(!empty($message)) {
            mail($to,"Message from www.myemail.com",$message1,$headers);
        } else {
            //echo 'fail';
            $err_message = '<font color="red">Please enter your message.</font>';
            $isError=true;
        }
    }


    ###### Forwarding to another page

    if(!empty($isError)) {
    } else {
        header("Location: thank_you.php");
    }
}



?>
4

2 回答 2

0

您的代码似乎是正确的,尽管您可以尝试如下 if 条件

if($isError) {
于 2013-10-30T08:39:41.553 回答
0

您的变量 $isError 是一个布尔值。所以你不必用 !isset() 检查它:

if($isError) {
   // error occured
} else {
  // send your message here
}

所以试试

if(!$isError) {

    if(!empty($message)) {
        mail($to,"Message from www.myemail.com",$message1,$headers);

        header("Location: thank_you.php");
    } else {
        //echo 'fail';
        $err_message = '<font color="red">Please enter your message.</font>';
        $isError=true;
    }
}

###### Forwarding to another page

/*if(!$isError) {
   header("Location: thank_you.php");  
}*/
于 2013-10-30T08:40:23.463 回答