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 :)
Link to comment
https://forums.phpfreaks.com/topic/288377-headers-allready-sent-problem/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.