Page 1 of 1

Session() Problems!

Posted: Tue May 27, 2008 2:35 pm
by dajawu
I am trying to make a simple login script that uses a session to save user info. My first problem was any time I called Session_start() in my file that contained HTML code it gave me this Warning:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/albraune/public_html/test.php:2) in /home/albraune/public_html/test.php on line 5

Now I searched for an answer to this problem and I found the answer. If I put session_start() between ob_start() and ob_end_clean() it wouldn't give me the warning, so now the beggining of my script looks like this:

Code: Select all

 
<?php
 
ob_start();
session_start();
ob_end_clean();
 
?>
 
But when I do that my session variable don't seem to be saved throughout the different pages. I set a simple variable that saves the username in $_SESSION['UN'] = $user and in another page I check $_SESSION['UN'] but it never seems to work.

Now I made a few test pages that had NO HTML in them so I didn't use ob_start() and ob_end_clean() and the variables in those test files worked fine. They got saved and restored in a different file flawlessly! But my code doesn't work for crap! Is ob_start() and ob_end_clean() screwing up my sessions? How should I do this the proper way?

Re: Session() Problems!

Posted: Tue May 27, 2008 2:52 pm
by dajawu
Sorry guys after reading the tutorial I read about a common newbie mistake that had the same Warring message as mine. It had to do with using header() after there already some output. So I am guessing session_start() is trying to send some header information? I put session_start() at the VERY top of the script and it seems to work. I would still like to understand why what I was doing wasn't working?

Re: Session() Problems!

Posted: Tue May 27, 2008 8:16 pm
by Jonah Bron
That's right. All header information must be sent before any output of any kind.