|
-
Aug 12th, 2009, 07:45 AM
#1
Thread Starter
Addicted Member
$_SESSION errors
I'm trying to use $_SESSION for the first time.
When testing my script in EasyPHP everything works OK but when I upload I keep getting this message...
Cannot send session cookie - headers already sent
This is how I've used the code
page 1
PHP Code:
session_start();
// defalt testing login
$username = "myname";
$password = "mypassword";
$_SESSION['username']= $username;
$_SESSION['password']= $password;
page 2
PHP Code:
$username = $_SESSION['username'];
$password = $_SESSION['password'];
echo $password;
echo $username;
-
Aug 12th, 2009, 07:59 AM
#2
Re: $_SESSION errors
Ensure nothing is being output to the browser before session_start();. If something is output, you will get the error you've mentioned.
-
Aug 12th, 2009, 10:39 AM
#3
Thread Starter
Addicted Member
Re: $_SESSION errors
how should I ensure nothing is being output to the browser?
by using session_destroy()?
-
Aug 12th, 2009, 11:05 AM
#4
Re: $_SESSION errors
You should be able to tell if your code is outputting something to the browser.
For example
PHP Code:
//breaks <?php echo "test"; session_start(); ?>
//also breaks <html> <?php session_start(); ?>
//Doesn't break <?php session_start(); ?> <html>
-
Aug 12th, 2009, 11:47 AM
#5
Re: $_SESSION errors
You also need to use session_start() at the top of every page in which you access session vars. So page 2 needs to be:
Code:
session_start();
$username = $_SESSION['username'];
$password = $_SESSION['password'];
echo $password;
echo $username;
In addition to kfcSmitty's examples, whitespace also counts as "output." So if you have a blank line prior to your <?php session_start(), get rid of that as well.
-
Aug 12th, 2009, 08:18 PM
#6
-
Aug 12th, 2009, 08:21 PM
#7
Re: $_SESSION errors
Also, because PHP does not support Unicode, encoding in UTF-8 with a byte-order-mark (BOM) will cause this problem because the BOM is considered output.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|