2012-11-25 77 views
-1

我得到這個問題的標題錯誤幫我看看什麼是錯誤的,我的聯繫方式:警告:的preg_match():未知的修飾詞「(」上線18

<?php 

//Prefedined Variables 
$to = "[email protected]"; 
$subject = "1"; 

if($_POST) { 
    // Collect POST data from form 
    $name = stripslashes($_POST['name']); 
    $email = stripslashes($_POST['email']); 
    $comment = stripslashes($_POST['comment']); 

    // Define email variables 
    $message = date('d/m/Y')."\n" . $name . " (" . $email . ") sent the following comment:\n" . $comment; 
    $headers = 'From: '.$email.'\r\n\'Reply-To: ' . $email . '\r\n\'X-Mailer: PHP/' . phpversion(); 

    //Validate 
    $header_injections = preg_match("(\r|\n)(to:|from:|cc:|bcc:)", $comment); 

    if(! empty($name) && ! empty($email) && ! empty($comment) && ! $header_injections) { 
     if(mail($to, $subject, $message, $headers)) { 
      return true; 
     } 
     else { 
      return false; 
     } 
    } 
    else { 
     return false; 
    } 
} 
?> 

看來問題是在這裏,但我不明白什麼是錯

$header_injections = preg_match("(\r|\n)(to:|from:|cc:|bcc:)", $comment); 

回答

3

嘗試:

$header_injections = preg_match("#(\r|\n)(to:|from:|cc:|bcc:)#", $comment); 

您必須提供在開始一個有效的符號在你的正則表達式結尾處,在這個例子中只是#,但你可以使用/或任何你想要的。

看一看這篇文章:RegEx delimiters

0

嘗試使用這樣的:

$header_injections = preg_match('/(\r|\n)(to:|from:|cc:|bcc:)/', $comment); 

也在你的IF條件,你應該檢查$header_injections這樣:

if(! empty($name) && ! empty($email) && ! empty($comment) && FALSE !== $header_injections) { 

由於的preg_match可以返回一個可強制轉換爲布爾值,並跳到你的驗證。

相關問題