problem with setcookie
there is one think, i can't understand anyway:((( when i try to set cookie(it is on line 28 in login.php), browser returns me an error!!!
Cannot modify header information - headers already sent by (output started at C:\xampp2\htdocs\video\index.php:9) in C:\xampp2\htdocs\video\login.php on line 28
b开发者_开发技巧ut on line 9 in index php, i haven't any header!!! there is a tag!!!
i cant understand it!!! can somebody tall me why it returns me such error?Cookies are sent as headers. From the PHP docs for 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 and tags as well as any whitespace.
Also, if your page is saved in UTF-8 format, the BOM (Byte Order Mark) will stop you from being able to set any headers, as it counts as output. See http://bugs.php.net/bug.php?id=22108. To get around this you need to save your file without the byte order mark.
See also: Byte order mark#Unwanted BOMs - Wikipedia
You can't print out anything on the site before sending a Header.
set the cookie first before any html tags (a.k.a output), even before declaring !DOCTYPE html or head, header information and the like. for example your file could look something like:
<?php setcookie("oreo", $value, time()+(60*60*24*30));?>
<?php setcookie("vanilla_wafer", $wafer, time()+(60*60*24*30));?>
<!DOCTYPE html>
<head>
<title>Cookies and Milk</title>
</head>
<body>
<p>yo</p>
</body>
</html>
Please post some code. What this error means is, that something was already sent (can also be an echo, error notice etc.).
You have to have your header functions at the very top of your application. Like basically the first lines are for header();
精彩评论