2012-02-25 86 views
0

我在http://www.kaimeramedia.com/derek/Website/contact.php。我检查了我的PHP代码在PHPcodechecker.com一个接触的形式,它说没有语法错误。我认为最初的代码在某个庄园里工作,因为每次我按下提交时,都会进入下一个验证,我将留下:“检测到不正确的电子邮件地址,请点击浏览器后退按钮,然后再试一次”。PHP触点形式验证工作不

我只是想知道我是否应该进行不同的编写代码,以确保电子邮件验证通过,并且允许提交的消息。我不知道我是否有向后编码的东西。我尝试了很多组合,但是就目前而言,这是我没有发生mailform.php错误而来的最远的组合。对于mailform.php文件的代码是:

<?php 
    session_start(); 
    $dontsendemail = 0; 
    $possiblespam = FALSE; 
    $strlenmessage = ""; 
    $email = $_REQUEST['email']; 
    $name = $_REQUEST['name']; 
    $message = $_REQUEST['message']; 
    $subject = "Regarding Your Portfolio";$emailaddress = "[email protected]"; 

    // checks if name field is empty 
    function checkname() { 
if (empty($name)) { 
    die ("You did not enter your name. Please hit your browser back button and try again."); 
return 1; 
      } 
     } 

// checks if the captcha is input correctly 
function checkcaptcha() { 
      if ($_SESSION["pass"] != $_POST["userpass"]) { 
       die("Sorry, you failed the CAPTCHA. Note that the CAPTCHA is case-sensitive. Please hit your browser back button and try again."); 
       return 1; 
      } 
     } 

// checks proper syntax 
function checkemail() { 
    if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $email)); 
    else{ 
     die("Improper email address detected. Please hit your browser back button and try again."); 
     return 1; 
    } 
} 


function spamcheck($field) { 
    if(eregi("to:",$field) || eregi("cc:",$field) || eregi("\r",$field) || eregi("\n",$field) || eregi("%0A",$field)){ 
     $possiblespam = TRUE; 
    }else $possiblespam = FALSE; 
    if ($possiblespam) { 
     die("Possible spam attempt detected. If this is not the case, please edit the content of the contact form and try again."); 
     return 1; 
    } 
} 
function strlencheck($field,$minlength,$whichfieldresponse) { 
    if (strlen($field) < $minlength){ 
     die($whichfieldresponse); 
     return 1; 
    } 



} 

     if ($dontsendemail == 0) $dontsendemail = checkcaptcha($email); 

if ($dontsendemail == 0) $dontsendemail = checkemail($email); 
if ($dontsendemail == 0) $dontsendemail = spamcheck($email); 
if ($dontsendemail == 0) $dontsendemail = spamcheck($name); 
if ($dontsendemail == 0) $dontsendemail = strlencheck($email,10,"The email address field is too short. Please hit your browser back button and check your entry.<br />"); 
if ($dontsendemail == 0) $dontsendemail = strlencheck($message,10,"The message field is too short. Please hit your browser back button and check your entry.<br />"); 
if ($dontsendemail == 0) $dontsendemail = strlencheck($emailaddress,8,"You have not selected a recipient of your message. Please hit your browser back button and check your entry.<br />"); 
if ($dontsendemail == 0) $dontsendemail = strlencheck($name,3,"The Name field is too short. Please hit your browser back button and check your entry.<br />"); 
if ($dontsendemail == 0) {mail($emailaddress,"Subject: $subject",$message,"From: $name,$email"); include "thankyou.php";} 
?> 

我放了假的电子邮件在此代码,但真正的一个是在mailform.php文件 如果任何人都可以看到我错在哪里这将是不胜感激。

+0

你可以上传电子邮件解决您的测试? – MichaelH 2012-02-25 04:22:35

+0

@MichaelH有那么重要吗? – giorgio 2012-02-25 04:29:48

+0

@giorgio我认为这可能是一个正则表达式问题。 – MichaelH 2012-02-25 04:39:06

回答

2

建立在我以前的答案上,我全力以赴清理脚本。我显然有太多时间在我手上。没有检查它是否有效。祝你好运!

<?PHP 
session_start(); 
try{ 
    $check = new check(); 
    if(!isset($_REQUEST['Email'])) 
     throw new exception('You did not enter an email address.'); 

    if(!isset($_REQUEST['message'])) 
     throw new exception('You did not enter a message.'); 

    if(!isset($_REQUEST['Name'])) 
     throw new exception('You did not enter a name'); 

    $sender = $_REQUEST['Email']; 
    $message = $_REQUEST['message']; 
    $name = $_REQUEST['Name']; 
    $recipient = '[email protected]'; 

    $subject = 'Regarding Your Portfolio'; 

    if($check->captcha('userpass') == FALSE) 
     throw new exception('Your captcha is incorrect.'); 

    if($check->spam($sender) == FALSE) 
     throw new exception('Your email field contains spam.'); 

    if($check->spam($name) == FALSE) 
     throw new exception('Your name field contains spam.'); 

    if($check->length($sender, 10) == FALSE) 
     throw new exception('Your email field does not satisfy the minimum character count.'); 

    if($check->length($message, 8) == FALSE) 
     throw new exception('Your message field does not satisfy the minimum character count.'); 

    if($check->length($name, 3) == FALSE) 
     throw new exception('Your name field does not satisfy the minimum character count.'); 

    mail($recipient, $subject, $message, "From: $name <$sender>"); 
    include "thankyou.php"; 
}catch (Exception $E){ 
    die($E->getMessage()); 
} 




class check{ 

    function captcha($field){ 
     if(isset($_REQUEST[$field])==FALSE){ return false; } 
     if($_SESSION['pass'] != $_REQUEST[$field]){ return false; } 
     return true; 
    } 

    function email($email){ 
     if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ return false;} 
     return true; 
    } 

    function spam($field){ 
     if(eregi("to:",$field) || eregi("cc:",$field) || eregi("\r",$field) || eregi("\n",$field) || eregi("%0A",$field)){ return false; } 
     return true; 
    } 

    function length($field, $min){ 
     if(strlen($field) < $min){ return false; } 
     return true; 
    } 
} 
+0

我想你上面的代码中都phpcodechecker.com和我做了位于www.kaimeramedia.com/derek/Website/mailform_test.php一个mailform_test.php页面和代码检查没有发现任何错误。 – 2012-02-25 13:41:27

+0

但是,当您加载contact.php页面并正确填写所有框并点击提交时,您将留下结果页面和文本:我在phpcodechecker.com上尝试了上面的代码,并且我在mailform_test.php页面www.kaimeramedia.com/derek/Website/mailform_test.php和代码检查器发现没有错误。但是,当您加载页面时,会自动发送到带有生成文本的新页面:您没有输入电子邮件地址。 – 2012-02-25 13:41:58

+0

我送你到错误的位置... www.kaimeramedia.com/derek/Website/contact_testnew.php – 2012-02-25 13:47:56

0

脚本有两个问题。

  1. 您不在$ email paramater中解析。你忘了包括它。
  2. 电子邮件的正则表达式非常挑剔,并不总是包含所有标准。

解决方案: 作为PHP5的,你可以使用下面的验证电子邮件,或者如果你想使用正则表达式刚才添加的$电子邮件paramater。

function checkemail($email) { 
    if(filter_var($email, FILTER_VALIDATE_EMAIL)){ 
    }else{ 
     die("Improper email address detected. Please hit your browser back button and try again."); 
     return 1; 
    } 
} 
+0

我想刚进入这个代码插入到原始mailform.php页面,但我仍然得到关于电子邮件相同的结果的源代码。试试www.kaimeramedia.com/derek/Website/contact.php。这封电子邮件验证是我身边的一个祸根! – 2012-02-25 13:54:42