Cannot send session cache limiter - headers already sent

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
hairytea
Forum Commoner
Posts: 92
Joined: Mon Feb 04, 2008 8:31 am

Cannot send session cache limiter - headers already sent

Post 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)??
lepad
Forum Newbie
Posts: 12
Joined: Fri Oct 05, 2007 9:58 am

Re: Cannot send session cache limiter - headers already sent

Post 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.
hairytea
Forum Commoner
Posts: 92
Joined: Mon Feb 04, 2008 8:31 am

Re: Cannot send session cache limiter - headers already sent

Post by hairytea »

sorry for my ignorance but im not 100% sure what you mean?
hairytea
Forum Commoner
Posts: 92
Joined: Mon Feb 04, 2008 8:31 am

Re: Cannot send session cache limiter - headers already sent

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