Forum Moderators: coopster

Message Too Old, No Replies

Session problem (session_start(); is in the right place.)

Heeeeelpppp

         

bassick

11:15 am on Sep 29, 2005 (gmt 0)

10+ Year Member



Ok having got this all working and error checking the form perfectly. I now run into a session problem when adding in the sessions so it can store/pass values to the next page. I know the below session code works fine as on the next page it works without a problem.

Can anyone see why this would cause the errors (i've taken out the full URL btw in the error statement):

Warning: Cannot send session cookie - headers already sent by (output started at /***URL***.php:1) in /***URL***.php on line 2

Warning: Cannot send session cache limiter - headers already sent (output started at /***URL***.php:1) in /***URL***.php on line 2

Warning: Cannot add header information - headers already sent by (output started at /***URL***.php:1) in /***URL***.php on line 3

Heres the code... point out the dumb mistake I made! :) as i guess its something really obvious that I can't see...

<?
session_start();
header("Cache-control: private");

if (!$username) { $username = $_SESSION['username']; }
else { $username = $_POST['username']; }
$_SESSION['username'] = $username;
?>

[edited by: ergophobe at 6:07 pm (utc) on Sep. 29, 2005]
[edit reason] no need to include code downstream of error [/edit]

omoutop

11:38 am on Sep 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi bassick!

First of all, before checking ur code remove ALL gaps or empty lines before starting script (<?) and at the end (?> or </html> or whatever)..there should not be any empty lines at the end and in the beginning...try this and post again

bassick

4:09 pm on Sep 29, 2005 (gmt 0)

10+ Year Member



any spacing/ line breaks through out the script won't cause the errors nor will it fix them, unless of course spaces are called inbetween quote or something like that.

the actual error it turns out is that after looking into it more session_start() will always return true as default. SO if I call it on a page and it has no value, or give it a value without loading the page it will cause an error. Since I wanted it to store a variable from a form on that page and begin the session with no value, on securer servers - those with register_globals off, the session will of course error!

Since there is nothing insecure about it, I can simply use @session_start(); to tell it to ignore the error/ lack of value.

Now that probably made no sense, but it works.. :) Course when I have time, I will look into a better fix.

ergophobe

6:05 pm on Sep 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You must be sending output, even just a blank like, to the screen before session start (that's what that error means).

You can't just suppress the error with @ and expect the session to work. The error is telling you that the headers have already been sent, so it's too late to set a cookie (which is what you are doing, among other things, when starting a session).

You need to figure out where your output is coming from and stop it, or use output buffering.