So I am trying to figure out cookies now. But for some reason something is not working. I have read over my book and have not found any obvious problems with the code.
Here is the entire script
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/xhtml1-strict.dtd">
<html>
<body>
<?php
if($_POST["sub"])
{
if(count($_COOKIE) > 0)
{
print "<div>No Cookie</div>";
}//end if count
else
{
print "<div>Cookie contains</div>";
foreach($_COOKIE as $key => $item)
{
print "<div>$key = $item</div>";
}//end for each
}//end else
foreach($_POST as $key=>$item)
{
if($key == "sub") {}
else
{
setcookie($key, $item, time()+(7200), "/");
}//end else
}//end foreach
}//end if sub
?>
<form action="cookie.php" method="post">
<input type="checkbox" name="one" value="2" />2<br />
<input type="checkbox" name="two" value="3" />3<br />
<input type="checkbox" name="three" value="4" />4<br />
<input type="checkbox" name="four" value="5" />5<br />
<input type="submit" name="sub" value="Enter" />
</form>
</body>
</html>
The point of the script is to take in multiple values from a check box list and put them in a cookie. The only sketchy part that I can see is calling the setcookie function over and over again. But whenever I run the script I get the following warning
browser wrote:Warning: Cannot modify header information - headers already sent by (output started at /var/www/learnphp/cookie.php:4) in /var/www/learnphp/cookie.php on line 24
Am I missing something here? Could someone please help me figure this out? In the long run I want to be able to add and remove values from a cookie. Is there any simple way to do that?
Thanks for the help.