views:

798

answers:

3

Hi,I am new to PHP, I practised PHP setcookie() just now and failed.

http://localhost/test/index.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>

    </head>
    <body>
     <?php
     $value = 'something from somewhere';

     setcookie("TestCookie", $value);
     ?>
    </body>
</html>

http://localhost/test/view.php

<?php
 // I plan to view the cookie value via view.php
 echo $_COOKIE["TestCookie"];

?>

But I failed to run index.php, IE warning like this.

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\test\index.php:9) in C:\xampp\htdocs\test\index.php on line 12

I enabled my IE 6 cookie no doubt.

Is there anything wrong on my procedure above? Thank you.

WinXP OS and XAMPP 1.7.3 used.

+4  A: 

You're sending some HTML before you set the cookie. The cookie must be set before sending any output, as it is sent with the response headers. Do this:

<?php
$value = 'something from somewhere';
setcookie("TestCookie", $value);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>

    </head>
    <body>

    </body>
</html>
Jack Sleight
+4  A: 

The warning is clear.

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\test\index.php:9) in C:\xampp\htdocs\test\index.php on line 12

Cookies are sent in the HTTP response header. Since the HTML content already started, you cannot go back to the header and add the cookie.

From http://php.net/setcookie:

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.

Move that setcookie statement before any HTML appears:

<?php
 $value = 'something from somewhere';

 setcookie("TestCookie", $value);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
....
KennyTM
@KennyTM, A further more question. I tested the code, It works well. But when I created another **viewMore.php** which using the same code as the **view.php**. But viewTwo.php located in **http://localhost/testMore/viewMore.php**.(different directory) I failed to `echo` the cookie value with viewMore.php, Otherwise the view.php still works well. Could you please tell me why?
Nano HE
@Nano: Set the `$path` parameter to `/`. See the doc.
KennyTM
+1  A: 

Cookies are sent in the headers of the transmission of the HTTP page. Once you give some output, you cannot modify these anymore.

The problem in your case lies in you outputting some of the HTML-document before trying to set the cookie.

There are a few ways to solve it; one of which is setting the cookie prior to outputting anything on the page like so

<?php
    $value = 'something from somewhere';
    setcookie("TestCookie", $value);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>

    </head>
    <body>

    </body>
</html>

Alternatively, you could buffer your output so that nothing gets written until you explicitly tell it to

<?php
    ob_start(); // Initiate the output buffer
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>

    </head>
    <body>
     <?php
         $value = 'something from somewhere';
         setcookie("TestCookie", $value);
     ?>
    </body>
</html>
<?php
    ob_end_flush(); // Flush the output from the buffer
?>

For more information on this last approach, take a look at the ob_start and ob_end_flush functions.

It might be useful to read about setcookie, too.

Sebastian P.