Page 1 of 1

Cannot send session cache limiter - headers already sent

Posted: Tue Mar 18, 2008 7:09 am
by hairytea
Hi,

Newbie to php here sorry......

I am learning php from a book called sams teach yourself php, mysql and apache. The current topic i am running through is setting and using session cookies.

I get the following error....

Cannot send session cache limiter - headers already sent

From looking through various forums etc i have discovered that this is usually caused by un-necessary white space that dreamweaver may have added.

My code.....

Code: Select all

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
session_start();
?>
<html>
<head>
<title>Storing an array with a session</title>
</head>
<body>
<h1>Product Choice Page</h1>
<?php
if (isset($_POST["form_products"])) {
    if (!empty($_SESSION["products"])) {
        $products = array_unique(
        array_merge(unserialize($_SESSION["products"]),
        $_POST["form_products"]));
        $_SESSION["products"] = serialize($products);
    } else {
        $_SESSION["products"] = serialize($_POST["form_products"]);
    }
    echo "<p>Your products have been registered!</p>";
}
?>
<form method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<P><strong>Select some products:</strong><br>
<select name="form_products[]" multiple="multiple" size="3">
<option value="Sonic Screwdriver">Sonic Screwdriver</option>
<option value="Hal 2000">Hal 2000</option>
<option value="Tardis">Tardis</option>
<option value="ORAC">ORAC</option>
<option value="Transporter bracelet">Transporter bracelet</option>
</select>
<P><input type="submit" value="choose"/></p>
</form>
<p><a href="session1.php">go to content page</a></p>
</body>
</html>
 
My conclusion.....

After several hours searching for white space that DID NOT exist in my code i decided to try and remove my Document Type Definition (<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">)!

After removing the DTD the script ran fine with no errors.....so my question is....

Is it really advisable to run pages without declaring a DTD (document type definition)??

Re: Cannot send session cache limiter - headers already sent

Posted: Tue Mar 18, 2008 7:12 am
by lepad
You should declare a doctype definition, but you might want to try setting the session before the first output. It should work as well and you shouldn't get the error.

Also, putting php code before your first output (that being your dtd) doesn't affect the source at all.

Re: Cannot send session cache limiter - headers already sent

Posted: Tue Mar 18, 2008 7:15 am
by hairytea
sorry for my ignorance but im not 100% sure what you mean?

Re: Cannot send session cache limiter - headers already sent

Posted: Tue Mar 18, 2008 7:19 am
by hairytea
lepad wrote:You should declare a doctype definition, but you might want to try setting the session before the first output. It should work as well and you shouldn't get the error.

Also, putting php code before your first output (that being your dtd) doesn't affect the source at all.
Like this you mean?........

Code: Select all

 
<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Storing an array with a session</title>
</head>
<body>
<h1>Product Choice Page</h1>
<?php
if (isset($_POST["form_products"])) {
    if (!empty($_SESSION["products"])) {
        $products = array_unique(
        array_merge(unserialize($_SESSION["products"]),
        $_POST["form_products"]));
        $_SESSION["products"] = serialize($products);
    } else {
        $_SESSION["products"] = serialize($_POST["form_products"]);
    }
    echo "<p>Your products have been registered!</p>";
}
?>
<form method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<P><strong>Select some products:</strong><br>
<select name="form_products[]" multiple="multiple" size="3">
<option value="Sonic Screwdriver">Sonic Screwdriver</option>
<option value="Hal 2000">Hal 2000</option>
<option value="Tardis">Tardis</option>
<option value="ORAC">ORAC</option>
<option value="Transporter bracelet">Transporter bracelet</option>
</select>
<P><input type="submit" value="choose"/></p>
</form>
<p><a href="session1.php">go to content page</a></p>
</body>
</html>