Page 1 of 1

error message regarding session id

Posted: Tue Sep 19, 2006 7:06 am
by nutstretch
I have a website i am working on which requires sessions. i ahve put the seeions at the top of the pages that require the information.

Code: Select all

session_start();
if(isset($_SESSION['cart']))
{
   $_SESSION['cart'] = $_SESSION['cart'];
   }
else
   {
    $_SESSION['cart'] = $today.(rand(0,32000)*rand(0,32000));
	}
Everything seems to work ok on my home server but when I upload i am getting this error
'
Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /var/www/html/test/longscarves.php:2) in /var/www/html/test/longscarves.php on line 3

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /var/www/html/test/longscarves.php:2) in /var/www/html/test/longscarves.php on line 3'

Does anyone know why this is happening and how i deal with it?

Thanks in anticipation

nuts

Posted: Tue Sep 19, 2006 7:45 am
by CoderGoblin
If you do a search for "headers already sent" on these forums you should find your answer. In brief however session_start() should be before you output anything (even a blank line before <?php will cause the error).

Re: error message regarding session id

Posted: Tue Sep 19, 2006 8:23 am
by GM
nutstretch wrote:I have a website i am working on which requires sessions. i ahve put the seeions at the top of the pages that require the information.

Code: Select all

session_start();
if(isset($_SESSION['cart']))
{
   $_SESSION['cart'] = $_SESSION['cart'];
   }
else
   {
    $_SESSION['cart'] = $today.(rand(0,32000)*rand(0,32000));
	}
Everything seems to work ok on my home server but when I upload i am getting this error
'
Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /var/www/html/test/longscarves.php:2) in /var/www/html/test/longscarves.php on line 3

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /var/www/html/test/longscarves.php:2) in /var/www/html/test/longscarves.php on line 3'

Does anyone know why this is happening and how i deal with it?

Thanks in anticipation

nuts
Yes - the problem is caused by whitespace before your opening <?php tag.

Your code can be improved upon:

Code: Select all

session_start();

if(!isset($_SESSION['cart'])) {
    $_SESSION['cart'] = $today.(rand(0,32000)*rand(0,32000));
}
There is no need to ever write something like

Code: Select all

$_SESSION['somevar'] = $_SESSION['somevar'];

Posted: Tue Sep 19, 2006 8:37 am
by nutstretch
thanks for the code sugesstion. I have checked the files and there is no space before the <?PHP.

the page it is happening on is one where the page deletes the selected item from the database and then sends the user back to the shopping cart which should then refresh.

the error i am getting is
Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/test/delete.php:16) in /var/www/html/test/delete.php on line 32

I have looked through the forum and all i could find was info about space before the session etc.

Any help appreciated

Nuts

Posted: Tue Sep 19, 2006 8:42 am
by GM
OK - make sure there is no output whatsoever before you start the session

this will cause errors:

Code: Select all

echo "anything at all";

session_start();
whereas this will not:

Code: Select all

session_start();

echo "anything at all";
If in doubt, move your session_start() right to the very top of your script.

Hope this helps.

Posted: Tue Sep 19, 2006 8:45 am
by feyd

Posted: Tue Sep 19, 2006 8:45 am
by happy_tapper
I had a similar sort of error on one of my programs. Each page, except the main page, had the lines :

Code: Select all

session_id();
session_start();
This was to retrieve the session information to use on the next pages.

However i had a page where i had 'session_start()' before the 'session_id()' and this brought up the same sort of error. I'm presuming it is trying to start another session within a session??

Don't know if it will help, but it could be worth looking out for...

Posted: Tue Sep 19, 2006 8:47 am
by nutstretch
it is right at the top of the page before anything. i have clicked to the left and there is nothing to delete.

here is the whole page code

Code: Select all

<?PHP
session_start(); 

if(!isset($_SESSION['cart'])) { 
    $_SESSION['cart'] = $today.(rand(0,32000)*rand(0,32000)); 
} 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>shopping basket</title>
</head>

<body>
<?php
$id = $_REQUEST['id'];

$sessionid = $_SESSION['cart'];
$status = "refresh";
$linkID = @mysql_connect("localhost", "username", "password");
mysql_select_db("womens-accessories_co_uk_-_womensaccessories", $linkID);

//$linkID = @mysql_connect("localhost", "root", "");
//mysql_select_db("womensaccesories", $linkID);
ini_set('display_errors', 1); 
error_reporting(E_ALL); 

$result =mysql_query("DELETE FROM shoppingcart WHERE session LIKE '$sessionid' AND item LIKE '$id'", $linkID)or die(mysql_error()); 
if ($result == TRUE)
{
$URL="cart.php?id=$status";header ("Location: $URL");
}
else
{
print "couldn't delete";
}

mysql_close($linkID);
?>
</body>
</html>

Posted: Tue Sep 19, 2006 8:54 am
by GM
feyd's link is what you need.

You are outputting all this:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<title>shopping basket</title> 
</head>
then doing this:

Code: Select all

header ("Location: $URL");
for header to work, nothing should have already been outputted to the browser.

Only write that stuff out to the page once you've completed all the various logic checks on the page.

Posted: Tue Sep 19, 2006 8:59 am
by nutstretch
Thanks I took out all that html rubbish as i realised it didn't need to be there as it was just redirecting after anyway and it worked without the warning.

Thanks to all who helped