3

可能重复:
PHP 已发送的标头

编辑** 我现在已经修复了已经发送到第 53 行的标题,即。我的 common.php 中的最后一行启动了会话,导致标头已经发送。那么现在我应该把 session_start 放在哪里呢?

我的 index.php -

<?php 
require("common.php"); 
if(empty($_SESSION['user'])) 
{ 
    header("Location: login.php"); 
    die("Redirecting to login.php"); 
}   
?> 
<!DOCTYPE html>

<!-- paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/ -->
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
 <!--[if IE 7]>    <html class="no-js lt-ie9 lt-ie8" lang="en"> <![endif]-->
<!--[if IE 8]>    <html class="no-js lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
<head>
<meta charset="utf-8" />

<!-- Set the viewport width to device width for mobile -->
<meta name="viewport" content="width=device-width" />

 <title>nCMS | Simplicity, Reimagined.</title>

 <!-- Included CSS Files (Uncompressed) -->
 <!--
 <link rel="stylesheet" href="stylesheets/foundation.css">
 -->

<!-- Included CSS Files (Compressed) -->
<link rel="stylesheet" href="stylesheets/foundation.min.css">
<link rel="stylesheet" href="stylesheets/app.css">

<script src="javascripts/modernizr.foundation.js"></script>

<!-- IE Fix for HTML5 Tags -->
<!--[if lt IE 9]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
 </head>
<body>
<!--INCLUDE HEADER -->

<?PHP include('header.php'); ?>


<!--/INCLUDE HEADER -->

<!--INCLUDE PAGE -->
<div class="row">
Hello <?php echo htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8'); ?>,     secret content!<br /> 


<h3>Hi, I'm Index!</h3>

</div>
 <!--/INCLUDE PAGE-->

  <!--INCLUDE FOOTER-->


<?PHP include('footer.php'); ?>

<!--/INCLUDE FOOTER-->
<!-- Included JS Files (Compressed) -->
<script src="javascripts/jquery.js"></script>
<script src="javascripts/foundation.min.js"></script>

<!-- Initialize JS Plugins -->
<script src="javascripts/app.js"></script>
</body>
</html>

这是实时页面 - http://www.cogameservers.com/ncms

感谢您的帮助 - 死灵


common.php -

<?php 


$username = ""; 
$password = ""; 
$host = ""; 
$dbname = ""; 


$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'); 

try 
{ 

    $db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); 
} 
catch(PDOException $ex) 
{ 

    die("Failed to connect to the database: " . $ex->getMessage()); 
} 


$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 


$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); 


if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) 
{ 
    function undo_magic_quotes_gpc(&$array) 
    { 
        foreach($array as &$value) 
        { 
            if(is_array($value)) 
            { 
                undo_magic_quotes_gpc($value); 
            } 
            else 
            { 
                $value = stripslashes($value); 
            } 
        } 
    } 

    undo_magic_quotes_gpc($_POST); 
    undo_magic_quotes_gpc($_GET); 
    undo_magic_quotes_gpc($_COOKIE); 
} 


header('Content-Type: text/html; charset=utf-8'); 


session_start();
4

2 回答 2

4

我知道您从我的评论中解决了您的问题。但这是答案。

正如您所问的,您将 session_start 放在哪里以删除已发送的标头错误。

作为一般做法,将 session_start() 放在 php 文件的开头。还需要清除以前的缓冲区。

ob_start(); // this will clear output buffer
session_start();
于 2012-09-29T18:19:50.320 回答
3

看来错误是由header("Location: login.php");- 错误说common.php已经在第 77 行发送标头。只要发送标头,它将继续这样做common.php

Warning: Cannot modify header information - headers already sent by (output started at /home/megatron/public_html/ncms/index.php:9) in /home/megatron/public_html/ncms/common.php on line 77

在不知道该文件的内容的情况下,我无法帮助您。我建议只查看该文件以查找发送一些标头的内容,看看是否有必要或以不同的方式解决此问题。

于 2012-09-29T17:35:21.407 回答