Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 集成验证码验证无效_Php_Validation_Captcha - Fatal编程技术网

Php 集成验证码验证无效

Php 集成验证码验证无效,php,validation,captcha,Php,Validation,Captcha,有人能帮我走出困境,指引我走上正确的道路吗 我有一个简单的表单和验证,但我的电子邮件中总是有垃圾邮件,然后我决定将验证码放入其中,以阻止机器人发送垃圾邮件 问题是,即使验证码是空白的消息仍然发送到我的电子邮件,我很难指出问题是什么,因为我仍然在学习PHP的过程中简而言之,我只有PHP的基本知识 顺便说一下,这是我的代码: HTML: <form method="post" action="#"> <div class="inputFields"> <

有人能帮我走出困境,指引我走上正确的道路吗

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

问题是,即使验证码是空白的消息仍然发送到我的电子邮件,我很难指出问题是什么,因为我仍然在学习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");
    }
}



?>

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

if($isError) {

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

所以试试看

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");  
}*/
if(!$isError){
如果(!empty($message)){
邮件($to,“来自www.myemail.com的邮件,$message1,$headers);
标题(“位置:thank_you.php”);
}否则{
//回声“失败”;
$err_message='请输入您的消息';
$isError=true;
}
}
######转发到另一页
/*如果(!$isError){
标题(“位置:thank_you.php”);
}*/

您遇到了什么错误。?我的问题是,即使captcha字段为空,消息仍会发送到我的电子邮件。请将脚本开头的$isError变量设置为false(感谢您的回答,但即使captcha字段为空,消息仍会发送到我的电子邮件)
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");  
}*/