Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: session_start() [function.session-start]: Cannot send session cache limiter

Tags:

php

session

I have a problem with Session_start() here :

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\pages\home.php:4) in C:\xampp\htdocs\charts\home-chart.php on line 2

and in home-chart.php in line 2 I wrote codes like this :

session_start();
.
.
.
echo ' username: '.$_SESSION['user_name'];

although with this warning i can get result of $_SESSION['user_name'] but when I try to clear this part of the code :

session_start();

I can't see any result in screen. so, what's your solution?

<?php
@session_start();
require_once '../class/chart.class.php';
$chart = new chart();
?>
<html>
    <head>
        <link href='../css/home-chart.css'  rel='stylesheet' type='text/css' />
    </head>
    <body>
        <div class='float_left' style="margin-bottom:20px;">
            <div class='float_left' style='line-height: 9.41px; font-size: x-small;'>0<br /></div>      <div class='float_left' style="background-image: url('../image/web/chart.png'); width: 367px; height: 226px; " >
                <!-- 1 --><div class='float_left float_left column' style='margin-left:2px;'>
                    <?php echo $chart->mysql_fetch($chart->daycal(-3)); ?>
                </div>  
                <!-- 2 --><div class='float_left float_left column'>
                    <?php echo $chart->mysql_fetch($chart->daycal(-2)); ?>
                </div>  
                <!-- 3 --><div class='float_left column' >
                    <?php echo $chart->mysql_fetch($chart->daycal(-1)); ?>
                </div>  
                <!-- 4 --><div class='float_left column' >
                    <?php echo $chart->mysql_fetch($chart->daycal(0)); ?>
                </div>  
                <!-- 5 --><div class='float_left column' >
                    <?php echo $chart->mysql_fetch($chart->daycal(1)); ?>
                </div>  
                <!-- 6 --><div class='float_left column' >
                    <?php echo $chart->mysql_fetch($chart->daycal(2)); ?>
                </div>  
                <!-- 7 --><div class='float_left column' >
                    <?php echo $chart->mysql_fetch($chart->daycal(3)); ?>
                </div>  
            </div>


            <div class='float_single_full' ></div>
            <div class='float_left bottom_chart' style="margin-left:10px;"><?php echo $chart->dayofweek(-3); ?></div>
            <div class='float_left bottom_chart'><?php echo $chart->dayofweek(-2); ?></div>
            <div class='float_left bottom_chart'><?php echo $chart->dayofweek(-1); ?></div>
            <div class='float_left bottom_chart'  style='font-weight:bold'><?php echo $chart->dayofweek(0); ?></div>
            <div class='float_left bottom_chart'><?php echo $chart->dayofweek(1); ?></div>
            <div class='float_left bottom_chart'><?php echo $chart->dayofweek(2); ?></div>
            <div class='float_left bottom_chart'><?php echo $chart->dayofweek(3);
                    echo ' username: ' . $_SESSION['user_name'];
                    ?></div>
        </div>
    </body>
</html>
like image 207
Mehdi Hosseini Avatar asked Sep 10 '11 18:09

Mehdi Hosseini


People also ask

What does session_start () do in PHP?

session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie. When session_start() is called or when a session auto starts, PHP will call the open and read session save handlers.

Do I need to use session_start on every page?

It must be on every page you intend to use. The variables contained in the session—such as username and favorite color—are set with $_SESSION, a global variable. In this example, the session_start function is positioned after a non-printing comment but before any HTML.

Where should the session_start () function be used?

Note: The session_start() function must be the very first thing in your document. Before any HTML tags. As for your question you can start session wherever you want, but beware that session must be started before any output. So it is considered a reasonable approach to start session at the top of a page.


1 Answers

If you even have blank lines before the <?php tag, then you can't set headers. Your start of file, with line numbers, should look like this:

1. <?php
2. session_start();
3. header('Cache-control: private');

The message says "headers sent at line 2", so you're outputting something (a space, a blank line, whatever) on line 4 of home.php

If this file is an include, you should put your session_start(); at the top of home.php instead, and then you don't need it in this file.

like image 115
Joe Avatar answered Sep 17 '22 13:09

Joe