Jump to content

Tonights question:


Snatch

Recommended Posts

Hi, i'm using the following script to display a different search bar depending on the users session state. The script works but i'm getting, Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent. If I delete the session_start() from the script the script no longer works.

 

I'm using the script as an include in my index page - I'm guessing this is part of the problem as the browser will of already read the html tags in the index page? If so how I fix this? Please help, Thanks!

 

<?php
session_start(); // start the session
if(!isset($_SESSION['user'])){
    // check if the user is logged in
    // the user is not logged in
    // just display the search box
?>
<script type="text/javascript" src="./js/cleardefault.js"></script>
<div id="search">
<form method="get" action="search.php">
<input name="search" type="text" size="40" value="click here to search" class="cleardefault" />
</form> 
</div>
<?php
} else {
    // the user is logged in
    // display the search box, name of user and logout
?>
<script type="text/javascript" src="./js/cleardefault.js"></script>
<div id="search">
<form method="get" action="search.php">
<input name="search" type="text" size="40" value="click here to search" class="cleardefault" />
You are currently logged in as <?php echo "$_SESSION[user]"; ?> <a href='logout.php'>Logout</a>
</form> 


</div>
<?php
}
?>




 

 

Link to comment
https://forums.phpfreaks.com/topic/65124-tonights-question/
Share on other sites

you're exactly right - the output in the includer is flushing the headers, making session_start() a violation.  you'll need to do one of two things to fix this.  you can either restructure your code (the better solution) such that all header calls and important processing take place before any output anywhere (ie. at the top of your includer), or you can use output buffering (a less recommended, "sweep-it-under-the-rug" solution).

 

to read more about output buffering, see ob_start() in the php manual.

Link to comment
https://forums.phpfreaks.com/topic/65124-tonights-question/#findComment-325023
Share on other sites

they are made before output in the INCLUDED file, but i'm referring to the INCLUDER file (that is, the one that includes the session_start() file).  move the session_start() call into the index.php file (or whatever the INCLUDER is) and you should eradicate the header warnings.

Link to comment
https://forums.phpfreaks.com/topic/65124-tonights-question/#findComment-325077
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.