Snatch Posted August 15, 2007 Share Posted August 15, 2007 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/65124-tonights-question/ Share on other sites More sharing options...
akitchin Posted August 15, 2007 Share Posted August 15, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/65124-tonights-question/#findComment-325023 Share on other sites More sharing options...
Snatch Posted August 15, 2007 Author Share Posted August 15, 2007 Hmm I seem to be making a real hash of this, I tried moving the include to the top of my index page which didn't make any differance. Do I need to restructure the include? I thought all header calls were made in it before output? Quote Link to comment https://forums.phpfreaks.com/topic/65124-tonights-question/#findComment-325062 Share on other sites More sharing options...
akitchin Posted August 15, 2007 Share Posted August 15, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/65124-tonights-question/#findComment-325077 Share on other sites More sharing options...
Snatch Posted August 15, 2007 Author Share Posted August 15, 2007 I'm with you now, thanks that work a treat! I was expecting it to be far more complex. Quote Link to comment https://forums.phpfreaks.com/topic/65124-tonights-question/#findComment-325084 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.