function setCookie(name,value)
{
   var Days = 30;
   var exp  = new Date();    //new Date("December 31, 9998");
       exp.setTime(exp.getTime() + Days*24*60*60*1000);
       document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function getCookie(name)
{
   var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
       if(arr=document.cookie.match(reg)) return unescape(arr[2]);
       else return null;
}
function delCookie(name)
{
   var exp = new Date();
       exp.setTime(exp.getTime() - 1);
   var cval=getCookie(name);
       if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}

解决方案 »

  1.   

    do not output any html before setcookie() or turn on output buffering, see
    http://www.php.net/manual/en/function.ob-start.php
      

  2.   

    感谢回复.
    现在知道出错的原因了,就是在setcookie("TestCookie","test",time()+3600);前面有echo "登陆成功!";出错了.
    还有一些问题不明白.
    是不是用户发帖子是都要调用COOKIE验证是否登陆?
    用户如果点"推出登陆"可以将其COOKIE设成无效,如果直接将浏览器关了,怎么处理?
    很菜的问题...->meizz(梅花雨): 你的代码是PHP还是JAVASCRIPT?呵呵~,不知道怎么应用.能给个说明吗?
      

  3.   

    建议使用session,更安全setcookie前面不能有任何输出空行,空格
    或者用
    ob_start();
    ...
    来控制缓存输出方式
      

  4.   

    setcookie前面不能有任何输出,包括代码中前面不能有空行,空格