php标题函数没有重定向并给我错误

时间:2013-12-27 00:26:03

标签: php http-redirect

我一直在非常密切地关注教程,并且我到达了一个部分,我收到了一条错误信息我已经检查了几次我的代码与完全相同的教程我已经研究了修复它的方法我已经尝试了不同的方法,没有,我不确定教程中使用的php版本是否过时。它似乎给我一个redirect_to header()函数的问题。

我得到的错误 未定义的索引:在第12行的C:\ wamp \ www \ widget_corp \ public \ create_subject.php中可见 注意:未定义的索引:在第18行的C:\ wamp \ www \ widget_corp \ includes \ validation_functions.php中可见 警告:无法修改标头信息 - 已在第3行的C:\ wamp \ www \ widget_corp \ includes \ functions.php中发送的标头(输出从C:\ wamp \ www \ widget_corp \ public \ create_subject.php:6开始)

 Heres my validation_functions.php

 <?php

 $errors = array();

 function fieldname_as_text($fieldname) {
 $fieldname = str_replace("_", " ", $fieldname);
 $fieldname = ucfirst($fieldname);
 return $fieldname;
}

function has_presence($value) {
 return isset($value) && $value !== "";
}

function validate_presences($required_fields) {
 global $errors;
 foreach($required_fields as $field) {
  $value = trim($_POST[$field]);
 if (!has_presence($value)) {
  $errors[$field] = fieldname_as_text($field) . " can't be blank";
  }
}
}

function has_max_length($value, $max) {
return strlen($value) <= $max;
}

function validate_max_lengths($fields_with_max_lengths) {
  global $errors;
// Expects an assoc. array
foreach($fields_with_max_lengths as $field => $max) {
 $value = trim($_POST[$field]);
 if (!has_max_length($value, $max)) {
   $errors[$field] = fieldname_as_text($field) . " is too long";
   }
  }
}
 // * inclusion in a set
 function has_inclusion_in($value, $set) {
  return in_array($value, $set);

}

function form_errors($errors=array()) {
 $output = "";
 if (!empty($errors)) {
  $output .= "<div class=\"error\">";
$output .= "Please fix the following errors:";
$output .= "<ul>";
foreach ($errors as $key => $error) {
   $output .= "<li>{$error}</li>";
 }
 $output .= "</ul>";
 $output .= "</div>";
}
return $output;
}
?>

Here is my create_subject.php
 <?php require_once("../includes/session.php"); ?>
<?php require_once("../includes/db_connection.php"); ?>
<?php require_once("../includes/functions.php"); ?>
<?php require_once("../includes/validation_functions.php"); ?>

<?php 
 if (isset($_POST['submit'])) {
 // Process the form

 $menu_name = mysql_prep($_POST["menu_name"]);
 $position = (int) $_POST["position"];
 $visible = (int) $_POST["visible"];

 // Validations
$required_fields = array("menu_name", "position", "visible");
validate_presences($required_fields);

$fields_with_max_lengths = array("menu_name" => 30);
validate_max_lengths($fields_with_max_lengths);

if (!empty($errors)) {
  $_SESSION["errors"] = $errors;
  redirect_to("new_subject.php");
}

      $query  = "INSERT INTO subjects (";
      $query .= " menu_name, position, visible";
      $query .= ") VALUES (";
      $query .= "  '{$menu_name}', {$position}, {$visible}";
      $query .= ")";
     $result = mysqli_query($connection, $query);
     // Test if there was a query error
     if ($result) {
        // Success
        $_SESSION["message"] = "Subject created.";
        redirect_to("manage_content.php");
    } else {
            // Failure
          $_SESSION["message"] = "Subject creation failed.";
          redirect_to("new_subject.php");
           }
    } else {
       // This is probably a GET request
       redirect_to("new_subject.php");
    }



  ?>

  <?php 
   if (isset($connection)) { mysqli_close($connection);} 
   ?>

  Here is my new_subject.php

  <?php require_once("../includes/session.php"); ?>
  <?php require_once("../includes/db_connection.php"); ?>
  <?php require_once("../includes/functions.php"); ?>
  <?php include("../includes/layouts/header.php"); ?>
  <?php find_selected_page(); ?>

<div id="main">
<div id="navigation">
<?php echo navigation($current_subject, $current_page); ?>
</div>
<div id="page">
<?php echo message(); ?>
<h2>Create Subject</h2>

<form action="create_subject.php" method="post">
<p>Menu name:
  <input type="text" name="menu_name" value="" />
  </p>
<p>Position:
 <select name="position">
 <?php
 $subject_set = find_all_subjects();
 $subject_count = mysqli_num_rows($subject_set);
    for($count=1; $count <= ($subject_count + 1); $count++) {
     echo "<option value=\"{$count}\">{$count}</option>";
 }
 ?>
 </select>
 </p>
 <p>Visible:
  <input type="radio" name="visible" value="0" /> No
  &nbsp;
  <input type="radio" name="visible" value="1" /> Yes
  </p>
<input type="submit" name="submit" value="Create Subject" />
</form> 
<br/>
<a href="manage_content.php">Cancel</a>
</div>
</div>
<?php include("../includes/layouts/footer.php");?>

And here is my redirect_to()
<?php
function redirect_to($new_location) {
    header("Location: " . $new_location);
    exit;
}   




  <!DOCTYPE>
 <html lang="en">
 <head>
 <title>Admin Page</title>
 <link href="stylesheets/public.css" media="all" rel="stylesheet" type="text/css"  
 />
 </head>
 <body>
 <div id="header">
<h1>Widget Corp</h1>
</div>
<div id="main">
<div id="navigation">
 <ul class="subjects"><li><a href="manage_content.php?subject=1">About Widget      
 Corp</a><ul class="pages"><li><a href="manage_content.php?page=1">Our Mission</a>   
 </li><li><a href="manage_content.php?page=2">Our History</a></li></ul></li><li><a  
 href="manage_content.php?subject=13"></a><ul class="pages"></ul></li><li><a  
 href="manage_content.php?subject=12"></a><ul class="pages"></ul></li><li><a  
 href="manage_content.php?subject=14"></a><ul class="pages"></ul></li><li><a  
 href="manage_content.php?subject=2">Products</a><ul class="pages"><li><a  
 href="manage_content.php?page=3">Large Widgets</a></li><li><a  
 href="manage_content.php?page=4">Small Widgets</a></li></ul></li><li><a  
 href="manage_content.php?subject=3">Services</a><ul class="pages"><li><a  
 href="manage_content.php?page=5">Retrofitting</a></li><li><a  
 href="manage_content.php?page=6">Certification</a></li></ul></li><li><a  
 href="manage_content.php?subject=8">Today's Widget Trivia</a><ul class="pages"> 
 </ul></li><li><a href="manage_content.php?subject=9">Sample Subject</a><ul  
 class="pages"></ul></li><li><a href="manage_content.php?subject=10">Test   
 Subject</a><ul class="pages"></ul></li><li><a  
 href="manage_content.php?subject=11">Test again</a><ul class="pages">   
 </ul></li></ul>    </div>
<div id="page">
    <h2>Create Subject</h2>

<form action="create_subject.php" method="post">
<p>Menu name:
<input type="text" name="menu_name" value="" />
</p>
<p>Position:
 <select name="position">
 <option value="1">1</option><option value="2">2</option><option   
 value="3">3</option><option value="4">4</option><option value="5">5</option><option  
 value="6">6</option><option value="7">7</option><option value="8">8</option><option  
 value="9">9</option><option value="10">10</option><option value="11">11</option>     
  </select>
 </p>
 <p>Visible:
  <input type="radio" name="visible" value="0" /> No
  &nbsp;
  <input type="radio" name="visible" value="1" /> Yes
  </p>
<input type="submit" name="submit" value="Create Subject" />
 </form>    
<br/>
<a href="manage_content.php">Cancel</a>
</div>
</div>

<div id="footer">Copyright 20xx, Widget Corp</div>  


 </body>
  </html>

3 个答案:

答案 0 :(得分:0)

在文本内容发送后,您无法发送标头。 HTTP协议无法正常工作。

由于visible不是$_POST中的索引(即未设置$_POST['visible']),因此PHP会将以下通知发送给浏览器:

  

注意:未定义的索引:在第12行的C:\ wamp \ www \ widget_corp \ public \ create_subject.php中可见   注意:未定义的索引:在第18行的C:\ wamp \ www \ widget_corp \ includes \ validation_functions.php中可见

稍后在您的脚本中,您正在尝试发送标头。显然,你不能这样做,因为上面的通知已经发送过了。如果您解决了“未定义索引”问题,那么发送标题应该没有问题。

执行此操作的一种方法是确保在将$_POST['visible']转换为整数之前设置if (isset($_POST['visible'])) $visible = (int) $_POST["visible"];

{{1}}

答案 1 :(得分:-1)

通常它是某个地方的换行符,可能在最初的&lt;?php标签之前......我已经多次遇到过这个问题。如果您的编辑器放置一个字节顺序标记(它不应该,但有些仍然可以),它可能会导致此消息。

无论哪种方式,你都在header命令之前输出一些东西。如果不是上述内容,可能是您的一个包含文件。检查页面的来源,你会在错误消息之前看到额外的字符(空格?)。

更新:

您的内容如下所示:

<?php include("file1.php");?>
<?php include("file2.php");?>

相反,你应该这样做:

<?php 
include("file1.php");
include("file2.php");
?>

最大的区别在于,这样您就不会在每次包含后添加换行符。

答案 2 :(得分:-1)

在处理redirect_to之前,请确保顶部没有断行/换行符。

例如

function has_presence($value) {
 return isset($value) && $value !== "";
}

function validate_presences($required_fields) {
 global $errors;
 foreach($required_fields as $field) {
  $value = trim($_POST[$field]);
 if (!has_presence($value)) {
  $errors[$field] = fieldname_as_text($field) . " can't be blank";
  }
}
}

应该是:

function has_presence($value) {
 return isset($value) && $value !== "";
}
function validate_presences($required_fields) {
 global $errors;
 foreach($required_fields as $field) {
  $value = trim($_POST[$field]);
 if (!has_presence($value)) {
  $errors[$field] = fieldname_as_text($field) . " can't be blank";
  }
}
}

请检查您包含的其他文件。

您可以替换:

redirect_to("new_subject.php");

为:

header('location: new_subject.php');