-1

Possible Duplicate:
PHP headers already sent
PHP session_start() error?

I'm getting this error:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\index.php:27) in C:\xampp\htdocs\connect.php on line 2

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\index.php:27) in C:\xampp\htdocs\connect.php on line 2

I'm don't have a clue what this means or how to handle it!! ANY help will be very appreciated!

Here is my 2 files that "causes" the problem:

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';
    ?>

Like I said, I really need to get this working so your help will mean a lot!

Community
  • 1
  • 1
Joe_B
  • 219
  • 3
  • 10
  • 19

6 Answers6

2

just what the error-message and the documentation* say: you have to call session_start(); before any output is sent to the browser. in your case, include connect.php at the very top of your index.php instead of somewhere in the middle.

alternatively, just add

<?php
session_start();
?>

at the top of index.php and leave out that line in connect.php.

*quoting the documentation:

Note:

To use cookie-based sessions, session_start() must be called before outputing anything to the browser.

oezi
  • 51,017
  • 10
  • 98
  • 115
1

You need to start the session before any output to the browser.

You've already output the document header etc before you include connect.php- you should initialise session_start(); before the line <!DOCTYPE HTML>, see the first usage note under the SESSION reference in the PHP docs.

SW4
  • 69,876
  • 20
  • 132
  • 137
1

You are not allowed to send output before setting the session. Put the include("connect.php") statement at the beginning of your index.php.

This error occurs because session_start() sends a cookie to the client to be able to identify the user. But cookies can only be sent to the browser if you haven't generated any output yet.

Thunraz
  • 570
  • 5
  • 18
1

You NEED to put the session_start() call before any code is sent to the client (at the start of the page).

Just put your include 'connect.php'; right at the start of index.php !

Aweb
  • 174
  • 3
  • 15
  • Ok I understand but there are a couple of files that I used include 'connect.php';, not just index.php. What must I do in that case?? – Joe_B Oct 14 '11 at 08:45
1

there are outputs in your index page before session_start(). Erase the session_start() from your connect.php and add it on the first line of your index page. it should work. or include your connect.php on the start of index page

ravi404
  • 7,119
  • 4
  • 31
  • 40
0

Just put ob_start(); above the session_start();. And at the end of the page, before </body>, put ob_end_flush();. I once got this error. By turning on output buffering you can solve this problem.

gmadd
  • 1,146
  • 9
  • 18
Sibu
  • 42
  • 6