-2

I have some trouble understanding why setcookie() doesn't work on several pages.

if (isset($_POST['group'])){ 
    $group = stripslashes($_POST['group']);
    setcookie(GSgroup, $group, time()+3600);
}

I use the above code to set a cookie when a form is posted with several options. I use this on an included page so that all pages which use this function will set the cookie to the right group.

Now, this worked perfectly fine when I tried it on localhost, but after uploading it to a live website it doesn't work anymore. The strange thing is, that after I used it on another included page it did work.

Does anyone have any clue how this could be? I am really confused.

If you need more information please ask.

4

3 回答 3

0
if(isset($_POST['group'])){ 
$group = stripslashes($_POST['group']);
setcookie(GSgroup, $group, time()+3600);
}

那是实际的代码吗?因为setcookie函数中的第一个参数应该是字符串或者包含字符串的变量。

于 2012-08-10T13:18:41.503 回答
0
setcookie('GSgroup', $group, time()+3600);
于 2012-08-10T13:19:26.177 回答
0

请记住,页面加载时会加载 cookie,因此如果您在 PHP 脚本的第 5 行设置了 cookie,setcookie然后在第 10 行访问它$_COOKIE不会给您 cookie 值。

您需要重新加载页面才能以这种方式访问​​ cookie 值。

我喜欢这个可用的 PHP cookie 类。

于 2012-08-29T10:24:49.117 回答