我把上面的改成:
<?php
$a=setcookie("myname","hello");
echo $a;
?>
<html>
<body>
<?
echo $_COOKIE["myname"];
?>
</body>
</html>
上网找到:
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.
<?php
$a=setcookie("myname","hello");
echo $a;
?>
<html>
<body>
<?
echo $_COOKIE["myname"];
?>
</body>
</html>
上网找到:
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.
比如你在a页面创建,你必须在b页面调用。
但也可以用下面的方法:
<?php
if (!empty($_COOKIE["myname"])) //如果不为空则证明有此cookie
echo $_COOKIE["myname"];
else
{
setcookie("myname","hello");//否则创建cookie
echo "<SCRIPT LANGUAGE='JAVASCRIPT'>window.location='".$_SERVER['PHP_SELF']."'</SCRIPT>";//创建完之后重定向到此页面
}
?>类似的还有其他方法
要是设置时间,到时间才失效。
如:
setcookie("myname","hello",time()+3600);//当前时间加上3600秒
一个小时后失效