Jump to content

headers allready sent problem


Aero77

Recommended Posts

I have this code, but I'm getting headers allready sent erros cause of the setcookie part. How can I fix this ?

 

Errors:

 

 

 

string(0) "" 
Warning: Cannot modify header information - headers already sent by (output started at /home/xxxxxxxx/public_html/index.php:27) in /home/xxxxxxxx/public_html/index.php on line 124
 
case "3":
	
  echo "
  <h2 align=\"center\">Arbeidstid - Start/Stop</h2>";
	$today = date("d-m-y"); 
	$time = date("H:i");
	$userid = $_COOKIE['userid'];  
	
	echo "<form action=\"index.php?fid=3\" method=\"post\">";

	if (isset($_POST['today'])) {
		
		$dhours = $_POST['dhours'];
		$output = str_replace(',', '.', $dhours);
			var_dump($output);	
		$dhours = ceil($output);

		if (isset($_COOKIE['wstart'])) { 
			$query=$oDB->Prepare("UPDATE workhours SET stop=:time, dhours=:dhours WHERE userid=:userid ORDER BY start DESC LIMIT 1"); 
				$query->execute(array(':userid' => $userid, ':time' => $time, ':dhours' => $dhours));		
			unset($_COOKIE['wstart']);
			setcookie("wstart", "", time()-3600);
		}
		else {  
			$query=$oDB->Prepare("INSERT INTO workhours (userid, date, start) VALUES (:userid, :today, :time)");
				$query->execute(array(':userid' => $userid, ':today' => $today, ':time' => $time));		
			setcookie("wstart", $today, time()+96000);			
		}

		echo "<p class=\"red\">Ny data er lagret! <a href=\"user.php\">[Klikk her for å komme til ditt brukerpanel].</a></p>";
			
		if (!$query) {
			echo "\nPDO::errorInfo():\n";
			print_r($oDB->errorInfo());
		}		
	} 
	elseif (!isset($_POST['today'])) {
		if (isset($_COOKIE['wstart'])) { 
			echo "<input type=\"hidden\" name=\"today\" value=\"$today\"><input type=\"text\" name=\"dhours\" placeholder=\"antall timer kjørt\" required /><input type=\"submit\" value=\"stop\" />";	
		}
		else { 
			echo "<input type=\"hidden\" name=\"today\" value=\"$today\"><input type=\"submit\" value=\"start\" />";
		}
	}
	
	else { echo "yeah yeah";}
	echo "</form>";
	break;
Thanks for reading. Hope someone can help :)
Edited by Aero77
Link to comment
Share on other sites

The manual will help.

 

 

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.

 

You cannot have output before setting a cookie. Since you have plenty of output before you finally call setcookie(), you need to restructure your application.

 

It's generally poor practice to heavily mix PHP and HTML (also known as “spaghetti code”). Put the application logic on top of the script and echo the output at the very end of the script. This will prevent problems like this in the future.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.