PHP - 由session_start()引起的已发送的标头;?

时间:2012-09-29 17:29:35

标签: php header

  

可能重复:
  Headers already sent by 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

感谢您的帮助 - Necro


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();

2 个答案:

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

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

答案 1 :(得分:3)

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

当你问到你把session_start放在哪里删除已发送错误的标题时。

通常的做法是将session_start()放在php文件的开头。还需要清除以前的bufffer。

ob_start(); // this will clear output buffer
session_start();