2012-02-25 9 views
0

문의 양식이 http://www.kaimeramedia.com/derek/Website/contact.php입니다. PHPcodechecker.com에서 PHP 코드를 확인했는데 구문 오류가 없다고합니다. 나는 제출 코드를 누를 때마다 다음 제출처로 넘어 가기 때문에 시작 코드가 일부 매너에서 작동하고 있다고 생각합니다 : "부적절한 이메일 주소가 발견되었습니다. 브라우저의 뒤로 버튼을 누르고 다시 시도하십시오".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 파일 에 다음 mailform.php 파일의 코드는 대단히 감사합니다.

+0

시험용 이메일 주소를 업로드 할 수 있습니까? – MichaelH

+0

@MichaelH는 그렇게 중요합니까? – giorgio

+0

@giorgio 정규식 문제 일 수 있다고 생각했습니다. – MichaelH

답변

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 페이지가 만들어졌으며 코드 검사기에서 오류가 발견되지 않았습니다. –

+0

그러나 contact.php 페이지를로드하고 모든 상자를 올바르게 채우고 제출하면 결과 페이지와 텍스트가 남습니다. phpcodechecker.com에서 위 코드를 시도하고 mailform_test.php 페이지를 다음 위치에 만들었습니다. www.kaimeramedia.com/derek/Website/mailform_test.php 및 코드 검사기에서 오류가 발견되지 않았습니다. 그러나 페이지를로드하면 결과 텍스트가있는 새 페이지로 자동 전송됩니다. 전자 메일 주소를 입력하지 않았습니다. –

+0

잘못된 위치로 보냈습니다 ... www.kaimeramedia.com/derek/Website/contact_testnew.php –

0

스크립트에 두 가지 문제점이 있습니다.

  1. $ email 매개 변수를 파싱하지 않습니다. 그것을 포함하는 것을 잊었습니다.
  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에서 직접 사용해보십시오. 이 전자 메일 유효성 검사는 저의 골칫거리입니다! –

관련 문제