-1

Possible Duplicate:
“Warning: Cannot modify header information - headers already sent by” error
setcookie, Cannot modify header information - headers already sent

I have this code to set cookie :

if (!isset($_COOKIE["CorkIU"])) {
setcookie("CorkIU", 2dd2ee3aUgsvoRye, time()+60*60*24*365, "/");
}

I have added this code in layout.php file

The first lines from layout.php file is

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<?
    include ("inc/css.php");

if (!isset($_COOKIE["CorkIU"])) {
setcookie("CorkIU", 2dd2ee3aUgsvoRye, time()+60*60*24*365, "/");
}

?>
<head>
<title>Welcome To Example</title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<link href="style/default.css" rel="stylesheet" type="text/css">
</head>

and here is the error :

Warning: Cannot modify header information - headers already sent by (output started at /home/ex/public_html/template/layout.php:4) in /home/ex/public_html/template/layout.php on line 8
Community
  • 1
  • 1
Maroman
  • 316
  • 1
  • 5
  • 13
  • Plenty of duplicates to help you debug your code. – Josh Mar 05 '12 at 17:41
  • Cookies must be set **before** making any kind of output. But this question has been asked so many times before that even Google knows the answer! – Quasdunk Mar 05 '12 at 17:42

2 Answers2

4

The error tells you everything you need to know:

headers already sent by (output

Move the call to setcookie to before you output any content (which you do here: <!DOCTYPE html)

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

From the PHP manual:

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 and tags as well as any whitespace.

j08691
  • 204,283
  • 31
  • 260
  • 272