-2

I have a problem with cookies, this is my code right below, but i get the Cannot modify headers error. I read that this happens once you send info to the user, but I didn't send anything. Did I?

Any help would be appreciated. Thanks!

<?php
$activities=array("Play Guitar","Write \"Timeless Legend\"","Develop","Dungeons and             Dragons Quest","Photography","Android"); 
$used[0]=null;
$used[1]=null;
$used[2]=null;
$used[3]=null;
$used[4]=null;
$used[5]=null;

if (isset($_COOKIE['cookiesSet'])){
for($h=0;$h<=5;$h++){
$used[$h]=$_COOKIE[$h];
}}
else{

for ($i=0; $i<=5;$i++){
$rand=rand(0,5);

while($used[$rand]!=null){
$rand=rand(0,5);}
$used[$rand]=$activities[$i];

 }

$midnight = strtotime('midnight', time());
for ($p=0;$p<=5;$p++){
setCookie($p,$used[$p],$midnight);
}
setCookie('cookiesSet','pollin',$midnight);
}
?>

<!DOCTYPE HTML>
<HTML>
<HEAD>
    <TITLE>Time Manager</TITLE>
Jo Colina
  • 1,870
  • 7
  • 28
  • 46
  • 1
    Have a look at the following post; I think it might be the blank space after ?> Check out http://stackoverflow.com/questions/8028957/headers-already-sent-by-php – SaganRitual Aug 03 '13 at 23:35

1 Answers1

0

setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace.

Netwidz
  • 179
  • 4