PHP - “通知:未定义的索引”错误

时间:2010-10-29 15:07:31

标签: php

注意:未定义索引:第12行/var/www/mailer.php中的主题注意:未定义索引:第13行/var/www/mailer.php中的消息提示:未定义索引:来自/ var / www第14行/mailer.php注意:未定义索引:第15行/var/www/mailer.php中的verif_box注意:未定义索引:第23行/var/www/mailer.php中的tntcon没有收到变量,此页面不能直接访问

以下是代码

<?php

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

// -----------------------------------------
//  The Web Help .com
// -----------------------------------------
// remember to replace you@email.com 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("abhijit.infogence@gmail.com", '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;
    }
?>

8 个答案:

答案 0 :(得分:1)

您正试图访问不存在的变量部分。如果存在,您可能需要在使用它们之前进行检查。

答案 1 :(得分:0)

错误只是因为消息$ _POST数组没有名为'message'的键。它可能来自表格尚未提交的事实。该错误只是一个通知,不会阻止程序运行。

答案 2 :(得分:0)

在解决某些特定字段之前,您应该检查$_POST包含的内容,然后查看isset函数。或者只是关闭display_errors;)

答案 3 :(得分: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这个函数来完成所有字段的工作。

答案 4 :(得分:0)

您似乎在没有通过POST方法提交表单的情况下调用此PHP文件。确保您的邮件表单具有正确的方法集:

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

您还应该在调用mail()之前清理用户输入(即删除换行符和标记),否则您会遇到麻烦。

答案 5 :(得分:0)

您的$ _POST和$ _COOKIE数组不包含这些索引。

执行:

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

查看那些数组中包含的内容

答案 6 :(得分: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
}

答案 7 :(得分: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行。