2013-01-13 41 views
0

我從2012年接觸到現成的contact form on TutWow。它使用2007年的Douglas Lovell's PHP e-mail validation來檢查電子郵件字段是否有效(甚至查詢DNS服務器以查看域是否有效)。 6歲時,問題就出現了 - 它還是相關的嗎?考慮到安全性,PHP表單驗證需要注意什麼?

一般來說,人們通常應該在mail()腳本中尋找哪些腳本以確保其安全,但佔用的空間最小? Lovell先生的劇本與TutWow的PHP配合是否合格?

這裏是組合的PHP:

<?php 

// Clean up the input values 
foreach($_POST as $key => $value) { 
    if(ini_get('magic_quotes_gpc')) 
    $_POST[$key] = stripslashes($_POST[$key]); 

    $_POST[$key] = htmlspecialchars(strip_tags($_POST[$key])); 
} 

// Assign the input values to variables for easy reference 
$name = $_POST["name"]; 
$email = $_POST["email"]; 
$message = $_POST["message"]; 

// Test input values for errors 
$errors = array(); 
if(strlen($name) < 2) { 
    if(!$name) { 
    $errors[] = "You must enter a name."; 
    } else { 
    $errors[] = "Name must be at least 2 characters."; 
    } 
} 
if(!$email) { 
    $errors[] = "You must enter an email."; 
} else if(!validEmail($email)) { 
    $errors[] = "You must enter a valid email."; 
} 
if(strlen($message) < 10) { 
    if(!$message) { 
    $errors[] = "You must enter a message."; 
    } else { 
    $errors[] = "Message must be at least 10 characters."; 
    } 
} 

if($errors) { 
    // Output errors and die with a failure message 
    $errortext = ""; 
    foreach($errors as $error) { 
    $errortext .= "<li>".$error."</li>"; 
    } 
    die("<span class='failure'>The following errors occured:<ul>". $errortext ."</ul></span>"); 
} 

// Send the email 
$to = "YOUR_EMAIL"; 
$subject = "Contact Form: $name"; 
$message = "$message"; 
$headers = "From: $email"; 

mail($to, $subject, $message, $headers); 

// Die with a success message 
die("<span class='success'>Success! Your message has been sent.</span>"); 

// A function that checks to see if 
// an email is valid 
function validEmail($email) 
{ 
    $isValid = true; 
    $atIndex = strrpos($email, "@"); 
    if (is_bool($atIndex) && !$atIndex) 
    { 
     $isValid = false; 
    } 
    else 
    { 
     $domain = substr($email, $atIndex+1); 
     $local = substr($email, 0, $atIndex); 
     $localLen = strlen($local); 
     $domainLen = strlen($domain); 
     if ($localLen < 1 || $localLen > 64) 
     { 
     // local part length exceeded 
     $isValid = false; 
     } 
     else if ($domainLen < 1 || $domainLen > 255) 
     { 
     // domain part length exceeded 
     $isValid = false; 
     } 
     else if ($local[0] == '.' || $local[$localLen-1] == '.') 
     { 
     // local part starts or ends with '.' 
     $isValid = false; 
     } 
     else if (preg_match('/\\.\\./', $local)) 
     { 
     // local part has two consecutive dots 
     $isValid = false; 
     } 
     else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) 
     { 
     // character not valid in domain part 
     $isValid = false; 
     } 
     else if (preg_match('/\\.\\./', $domain)) 
     { 
     // domain part has two consecutive dots 
     $isValid = false; 
     } 
     else if(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', 
       str_replace("\\\\","",$local))) 
     { 
     // character not valid in local part unless 
     // local part is quoted 
     if (!preg_match('/^"(\\\\"|[^"])+"$/', 
      str_replace("\\\\","",$local))) 
     { 
      $isValid = false; 
     } 
     } 
     if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) 
     { 
     // domain not found in DNS 
     $isValid = false; 
     } 
    } 
    return $isValid; 
} 

?> 
+1

根據我的經驗,您可以通過查詢dns丟失有效地址。害怕。 –

+1

不會說安全方面,但可能做出五個正則表達式調用並檢查dns?糖蜜會變慢。 – rdlowrey

回答

1

這個驗證不是我見過的最好的。我通過快速看它注意的是:

  • 它採用htmlspecialchars()沒有編碼參數
  • 它採用strip_tags()一個原因,我看不出
  • 它使用的strlen代替mb_strlen
  • 這是容易遭受標題注入
  • mail validation不是我見過的最好的
  • 沒有CSRF標記
  • Theer is no captcha
0

通過我的所有項目,我發現this guide是最有幫助的安全明智的。

它有許多部分,包括表格處理,數據庫和SQL,會話等。

1

也許最安全的方法是發送發件人發送一封電子郵件,要求他們確認他們確實發送了郵件。此外,爲了防止您的聯繫表格變成垃圾郵件系統,每個IP地址可以提交的合同號碼數量限制爲每24小時1個,假設回覆將通過常規電子郵件完成。

+0

偉大的想法。是的,通過定期的電子郵件回覆。你知道一個現成的腳本,所有這一切? – Baumr

相關問題