2010-10-29 27 views
0

注意:未定義的索引:第12行的/var/www/mailer.php中的主題注意:未定義的索引:第13行的/var/www/mailer.php中的消息注意:未定義的索引:從第14行的/var/www/mailer.php注意:注意:未定義的索引:第15行中的/var/www/mailer.php中的verifier。注意:未定義的索引:tntcon在/var/www/mailer.php上線23沒有收到任何變量,當前頁不能被直接訪問PHP - 「Notice:Undefined index」error

下面是代碼

<?php 

ini_set('display_errors',1); 
error_reporting(E_ALL|E_STRICT); 

// ----------------------------------------- 
// The Web Help .com 
// ----------------------------------------- 
// remember to replace [email protected] with your own email address lower in this code. 

// load the variables form address bar 
$subject = $_POST["subject"]; 
$message = $_POST["message"]; 
$from = $_POST["from"]; 
$verif_box = $_POST["verif_box"]; 

// remove the backslashes that normally appears when entering " or ' 
$message = stripslashes($message); 
$subject = stripslashes($subject); 
$from = stripslashes($from); 

// check to see if verificaton code was correct 
if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){ 
    // if verification code was correct send the message and show this page 
    mail("[email protected]", 'TheWebHelp.com Form: '.$subject, $_SERVER['REMOTE_ADDR']."\n\n".$message, "From: $from"); 
    // delete the cookie so it cannot sent again by refreshing this page 
    setcookie('tntcon',''); 
} else if(isset($message) and $message!=""){ 
    // if verification code was incorrect then return to contact page and show error 
    header("Location:".$_SERVER['HTTP_REFERER']."?subject=$subject&from=$from&message=$message&wrong_code=true"); 
    exit; 
} else { 
    echo "no variables received, this page cannot be accessed directly"; 
    exit; 
    } 
?> 
+0

*(tipp)*使用'error_reporting(-1);'會顯示每個可能的錯誤,即使在未來的PHP版本中添加了新的級別和常量時也是如此。 – Gordon 2010-10-29 15:13:42

+0

有沒有聽說過?:D第二,這些是通知的 - 沒有錯誤,你可能試圖訪問部分$ _POST,這不存在 – Hannes 2010-10-29 15:15:00

回答

0

錯誤很簡單,因爲該消息$ _POST陣列不具有鍵稱爲「消息」。這可能來自表單尚未提交的事實。該錯誤只是一個通知,並不會阻止程序運行。

0

在處理某些特定領域之前,您應該檢查一下$_POST包含的內容,看看isset函數。或者乾脆關掉display_errors

+0

「或者乾脆關閉display_errors;)」=>我會建議只做一箇舊的/蹩腳的PHP應用程序在最近的服務器上工作... – 2010-10-29 15:18:23

+0

@Frosty,我會建議在任何生產服務器上;)你不想向你的訪問者顯示PHP錯誤:)舊PHP代碼上的代碼,是的,噸已棄用的通知。但我有時討厭通過isset檢查,並在'$ _POST'之前附加一個'@'來避免這些通知。 – kovshenin 2010-10-29 15:21:54

+0

我當時在談論dev服務器,因爲隱藏通知在那裏通常不是很好的做法。但我同意有時使用'isset()'檢查真的很無聊:-) – 2010-10-29 15:33:06

0

檢查用戶提交的數據

$subject = (isset($_POST["subject"]) ? $_POST["subject"] : ''); 
$message = (isset($_POST["message"]) ? $_POST["message"] : ''); 
$from = (isset($_POST["from"]) ? $_POST["from"] : ''); 
$verif_box = (isset($_POST["verif_box"]) ? $_POST["verif_box"] : ''); 

你甚至可以讓自己的函數來做到這一點

function checkPost($fieldname) 
{ 
    return (isset($_POST[$fieldname]) ? $_POST[$fieldname] : ''); 
} 

然後做

$subject = checkPost("subject"); 

我也推薦你檢查所需的POST字段

if (!isset($_POST["xxx"]) || trim($_POST["xxx"]) == '') 
{ 
    // throw exception, display error... 
} 

僅供參考,而不是使用stripslashes()函數,以避免「magic_quotes的」,你可以用一個簡單的功能,比如這個http://snippets.dzone.com/posts/show/5256這將爲各個領域做的工作。

0

看起來你不用通過POST方法提交表單就調用這個PHP文件。請確保您的郵件表格有正確的方法設置:

<form method="POST" action="yourfile.php"> 
etc. 
</form> 

你也應該調用郵件前消毒用戶輸入()(。即除去換行和標籤),否則你都在呼籲麻煩。

+0

可以...任何一個更改上面的代碼,這將對我有用...並讓我知道相同...請 – AON 2010-10-29 15:23:37

0

您的$ _POST和$ _COOKIE數組不包含這些索引。

務必:

print_r($_POST); 
print_r($_COOKIE); 

,看看什麼是包含在這些陣列

0
foreach(array('subject', 'message', 'from', 'verif_box') as $val) 
{ 
    if (isset($_POST[$val])) 
    { 
     $$val = trim($_POST[$val]); 
     continue; 
    } 

    // some sort of error checking, like telling the end user that 
    // not all fields were correctly given 
} 
0
//checking if array elements are set and changing variables values if so 
$subject = isset($_POST["subject"])?$_POST["subject"]:null; 
$message = isset($_POST["message"])?$_POST["message"]:null; 
$from = isset($_POST["from"])?$_POST["from"]:null; 
$verif_box = isset($_POST["verif_box"])?$_POST["verif_box"]:null; 

// check to see if verificaton code was correct and if cookie value on 'tntcon' is set 
if(isset($_COOKIE['tntcon']) && md5($verif_box).'a4xn' == $_COOKIE['tntcon']){ 

變化是在12-15行,並在23

+0

什麼關於只發布已更改的行,可能前後有1-2行的上下文? – 2011-11-14 16:52:28

+0

你是對的,謝謝。 – Constantine 2011-11-15 07:33:33

1

你試圖訪問不存在的變量的部分。如果它們存在,您可能需要在使用它們之前進行檢查。