Session() Problems!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dajawu
Forum Commoner
Posts: 59
Joined: Fri May 23, 2008 10:16 am

Session() Problems!

Post 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?
dajawu
Forum Commoner
Posts: 59
Joined: Fri May 23, 2008 10:16 am

Re: Session() Problems!

Post 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?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Session() Problems!

Post by Jonah Bron »

That's right. All header information must be sent before any output of any kind.
Post Reply