error message regarding session id

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
nutstretch
Forum Contributor
Posts: 104
Joined: Sun Jan 11, 2004 11:46 am
Location: Leicester

error message regarding session id

Post 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
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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).
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Re: error message regarding session id

Post 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'];
nutstretch
Forum Contributor
Posts: 104
Joined: Sun Jan 11, 2004 11:46 am
Location: Leicester

Post 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
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
happy_tapper
Forum Newbie
Posts: 5
Joined: Mon Sep 18, 2006 5:51 am
Location: UK

Post 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...
nutstretch
Forum Contributor
Posts: 104
Joined: Sun Jan 11, 2004 11:46 am
Location: Leicester

Post 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>
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post 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.
nutstretch
Forum Contributor
Posts: 104
Joined: Sun Jan 11, 2004 11:46 am
Location: Leicester

Post 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
Post Reply