2013-04-06 170 views
0

我遇到了我的PHP代碼問題。我使用IF-ELSE來檢查一切正常,但它一直給我「你沒有輸入收件人」。發送郵件[PHP]

<?php 

$to=trim($_POST['toperson']); 
$from=trim($_POST['spoofrom']); 
$message=trim($_POST['message']); 
$subject=trim($_POST['subj']); 


if (substr_count($to, '@') <= 1) { 
    if (!isset($to)) { 
     if (!isset($from)) { 
      if (!isset($subject)) { 
       if (!isset($message)) { 
        mail($to, $subject, $message, "From: " . $from); 
        print "Message was sent!"; 
       }else {print "You did not enter a message";} 
      }else {print "You did not enter a subject";} 
     }else {print "You did not enter a from email";} 
    }else {print "You did not enter a recipient";} 
}else{print "You entered 2 or more emails.";} 

?> 
+0

您是否在表單中檢查收件人的文本框名稱?也許這是錯誤的。 – zkanoca 2013-04-06 12:09:56

+1

請了解郵件標題注入。 – 2013-04-06 12:10:39

回答

1

嘗試

通過if (isset($to)) 更換您的條件if (!isset($to))並添加空管檢查

文件: http://php.net/manual/en/function.isset.php

http://www.php.net/manual/en/function.empty.php

像這樣:如果你的所有的表單數據爲空

if (substr_count($to, '@') <= 1) { 
    if (isset($to) && !empty($to)) { 
     if (isset($from) && !empty($from)) { 
      if (isset($subject) && !empty($subject)) { 
       if (isset($message) && !empty($message)) { 
        mail($to, $subject, $message, "From: " . $from); 
        print "Message was sent!"; 
       }else {print "You did not enter a message";} 
      }else {print "You did not enter a subject";} 
     }else {print "You did not enter a from email";} 
    }else {print "You did not enter a recipient";} 
}else{print "You entered 2 or more emails.";} 
+0

,這沒有任何意義。由於已經有一個assigment'$ to = ...;',因此isset-call總是正確的(如果密鑰存在,例如它不是null)。檢查空... – bwoebi 2013-04-06 12:12:48

+0

@bwoebi:確實。如果URL參數不存在,則分配將失敗並顯示'未定義索引'錯誤消息。 – 2013-04-06 12:14:21

+0

是的,這就是我所說的:「(如果密鑰存在,例如它不爲空)」。順便說一句。我不認爲他想檢查表單是否被操縱,但是如果所有的字段都被填滿了。 – bwoebi 2013-04-06 12:15:54

0

你最終的意思是:empty()而不是isset(),因爲它總是設置...但不總是填充?

或檢查isset($_POST['to'])和其他鍵,但不分配變量使用isset(支票is_null/=== null是更好的有

0

您的代碼將發送郵件。 if(!isset($_POST["something"]) 表示如果您的數據未設置,條件過程將觸發。

刪除感嘆號。

0

確保您的表單使用正則表達式進行了適當的驗證& |表單提交之前的html5/css3。 !使用空()詩isset()函數最後,我建議把

if (mail($to, $subject, $message, "From: " . $from)) { 
    print "Message was sent!"; 
} else { print "email failed to send!"; } 

而且,我把發件人:設置當變量不調用它,但是這更多的個人喜好。

$from= "From: trim($_POST['spoofrom'])";