当我想在php中启动会话时,出现了一个非常烦人的错误


Getting a really annoying error when I want to start a session in php

可能重复:
PHP标头已发送
PHP session_start((错误?

我得到这个错误:

警告:session_start(([function.session start]:无法发送会话cookie-标头已由发送(输出开始于C: ''xamplep''htdocs''index.php:27(,位于第2行的C:''xamplep''tdocs''connect.php中

警告:session_start(([function.session start]:无法发送会话缓存限制器-标头已发送(输出开始于C: ''xamplep''htdocs''index.php:27(,位于第2行的C:''xamplep''tdocs''connect.php中

我不知道这意味着什么,也不知道如何处理!!任何帮助都将不胜感激!

这是我的2个"导致"问题的文件:

connect.php

<?php
session_start();
$server = 'localhost';
$username = 'root';
$password = '';
$database = 'mydatabase';
if(!mysql_connect('localhost', 'root', ''))
{
    exit('Error: could not establish database connection');
}
if(!mysql_select_db($database))
{
    exit('Error: could not select the database');
}
?>

index.php

<!DOCTYPE HTML>
<head>
    <title> ShareLink </title>
    <link rel="stylesheet" href="style.css" type="text/css">
    <link rel="stylesheet" type="text/css" media="screen,projection" href="css/ui.totop.css" />
    <script src="js/jquery-1.3.2.min.js" type="text/javascript"></script>
    <script src="js/easing.js" type="text/javascript"></script>
    <script src="js/jquery.ui.totop.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            var defaults = {
                containerID: 'moccaUItoTop', // fading element id
                containerHoverClass: 'moccaUIhover', // fading element hover class
                scrollSpeed: 1200,
                easingType: 'linear' 
            };

            $().UItoTop({ easingType: 'easeOutQuart' });
        });
    </script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
</head>
<body>
    <?php
    include 'connect.php';
    include 'header.php';
    $sql = "SELECT
                categories.cat_id,
                categories.cat_name,
                categories.cat_description,
                COUNT(topics.topic_id) AS topics
            FROM
                categories
            LEFT JOIN
                topics
            ON
                topics.topic_id = categories.cat_id
            GROUP BY
                categories.cat_name, categories.cat_description, categories.cat_id";
    $result = mysql_query($sql);
    if(!$result)
    {
        echo 'The categories could not be displayed, please try again later.';
    }
    else
    {
        if(mysql_num_rows($result) == 0)
        {
            echo '<br/>';
            echo '<h3>Welcome to ShareLink! Here you can rate and share links with other users.</h3>';
            echo '<br/>';
            echo 'Please <a href="signup.php"><b>register</b></a> or  <a href="signin.php"><b>log in</b></a> to start sharing your links...';
            echo '<br/><br/>';
        }
        else
        {
            //prepare the table
            echo '<table border="1">
                  <tr>
                    <th>Category</th>
                    <th>Last topic</th>
                  </tr>';   
            while($row = mysql_fetch_assoc($result))
            {               
                echo '<tr>';
                    echo '<td class="leftpart">';
                        echo '<h3><a href="category.php?id=' . $row['cat_id'] . '">' . $row['cat_name'] . '</a></h3>' . $row['cat_description'];
                    echo '</td>';
                    echo '<td class="rightpart">';
                    //fetch last topic for each cat
                        $topicsql = "SELECT
                                        topic_id,
                                        topic_subject,
                                        topic_date,
                                        topic_cat
                                    FROM
                                        topics
                                    WHERE
                                        topic_cat = " . $row['cat_id'] . "
                                    ORDER BY
                                        topic_date
                                    DESC
                                    LIMIT
                                        1";
                        $topicsresult = mysql_query($topicsql);
                        if(!$topicsresult)
                        {
                            echo 'Last topic could not be displayed.';
                        }
                        else
                        {
                            if(mysql_num_rows($topicsresult) == 0)
                            {
                                echo 'no topics';
                            }
                            else
                            {
                                while($topicrow = mysql_fetch_assoc($topicsresult))
                                echo '<a href="topic.php?id=' . $topicrow['topic_id'] . '">' . $topicrow['topic_subject'] . '</a> at ' . date('d-m-Y', strtotime($topicrow['topic_date']));
                            }
                        }
                    echo '</td>';
                echo '</tr>';
            }
        }
        //rate the link
                //echo '<tr><td><b>Rate this link</b></td></tr>';
    }
    //include 'footer.php';
    ?>

就像我说的,我真的需要把这件事做好,这样你的帮助将意义重大!

错误消息和文档*所说的:在将任何输出发送到浏览器之前,必须调用session_start();。在您的情况下,将connect.php包含在index.php的最顶部,而不是在中间的某个位置。

或者,只需添加

<?php
session_start();
?>

在CCD_ 4的顶部并且在connect.php中省略该行。

*引用文档:

注意:

若要使用基于cookie的会话,必须先调用session_start((将任何内容输出到浏览器。

在向浏览器输出任何内容之前,您需要启动会话。

在包含connect.php之前,您已经输出了文档标题等-您应该在<!DOCTYPE HTML>行之前初始化session_start();,请参阅php文档中SESSION引用下的第一个用法说明。

在将任何代码发送到客户端之前(在页面的开头(,您需要放置session_start()调用。

只需将include 'connect.php';放在index.php的开头!

在设置会话之前,不允许发送输出。将include("connect.php")语句放在index.php的开头。

出现此错误是因为session_start()向客户端发送cookie以能够识别用户。但是,只有在您还没有生成任何输出的情况下,cookie才能发送到浏览器。

session_start()之前的索引页中有输出。从connect.php中删除session_start(),并将其添加到索引页的第一行。它应该起作用。或者在索引页的开头包含您的connect.php

只需将ob_start();置于session_start();之上。在页面末尾,在</body>之前,放入ob_end_flush();。我曾经犯过这个错误。通过打开输出缓冲,可以解决这个问题。