2013-10-30 55 views
0

有人可以帮助我,引导我正确的道路验证不行集成CAPTCHA

我有一个简单的形式和验证,但我在我的电子邮件了总垃圾邮件,然后我决定把CAPTCHA到它停止机器人发送垃圾邮件。

问题是即使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 = '[email protected]'; 

    $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"); 
    } 
} 



?> 
+1

你得到什么错误? –

+0

我的问题是甚至captcha字段是空的仍然消息发送到我的电子邮件。 – jhunlio

回答

0

您的代码似乎是正确的,虽然事件可以尝试像下面如果条件

if($isError) { 
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"); 
}*/ 
+0

谢谢你,我会试试 – jhunlio

+0

将你的脚本开头的$ isError变量设置为false(在<?php insert $ isError = false;之后)。否则你会得到一个PHP错误。 – TiMESPLiNTER

+0

谢谢你的答案,但仍然发送消息发送到我的电子邮件,即使captcha字段为空 – jhunlio