【发布时间】:2011-05-24 14:04:57
【问题描述】:
注意:这个问题已经解决了, 最后我发现它不是饼干 问题,问题出在 反序列化()函数。连载的 作为参数的 cookie 该函数必须是stripslash-ed 首先。
您好,我有一个关于 PHP Cookies 的问题。我正在使用 PHP Cookie 来保存用户偏好。我已经在本地机器(使用 XAMPP 的本地主机)上测试了我的代码。一切都很好,包括饼干。但是当我将它上传到实时服务器时,cookies 根本不起作用。似乎 setcookie() 函数没有写入 cookie 值。我已经通过在本地主机和实时服务器上回显 cookie 值进行了测试。 localhost 上的 $_COOKIE[] 值正在显示,但与实时服务器中的值不同。
我认为这可能与 $expire 时区有关,就像这篇文章 http://anupraj.com.np/index.php/php-cookies-not-working-php-cookie-tutorial-and-scirpt/14 中的时区一样。但后来我意识到我已将 cookie 设置为在 1 个月内过期,而不仅仅是在博客文章中的一小时内。所以我认为情况并非如此。
这是setting.php的内容
<?php
$defaultSettings['default_post_to'] = 'both';
$defaultSettings['timesince_style'] = 'simplify';
...
$defaultSettings['display_geo_info'] = 'true';
$defaultSettings['enable_javascript'] = 'true';
if(!isset($_COOKIE['settings'])){
setcookie("settings", serialize($defaultSettings), time()+3600*24*30);
header('Location: index.php');
}
$setting = unserialize($_COOKIE['settings']);
?>
这是 index.php 的内容
<?php
/*
ini_set ("display_errors", "1");
error_reporting(E_ALL);
*/
session_start();
require_once("settings.php"); // Settings files
require_once('varlib.php'); // Get all possible passed variable
require_once('auth.php'); // Check for channel login status
// If inputbar form submitted
if( $_POST['inputbox'] ){
...
}
else{
echo "SETTING COOKIE: <br/><br/>";
// This print_r is only showing the $_COOKIE value (which is stored on $setting) on localhost but no on live server
print_r($setting);
switch( $com ){
...
}
}
?>
我到处搜索(谷歌、stackoverflow、在 twiiter/FB 上询问朋友)仍然没有解决方案
我希望有人可以在这里给我解决方案 谢谢:)
【问题讨论】:
-
你试过setcookie()的路径和域参数了吗?
-
如果启用 error_reporting,您不会收到任何错误消息?
-
@Kamal:比较本地和实时服务器的
phpinfo()输出的session部分(其中还显示了cookie 设置)。 -
@rik: 还没有,我马上试试
-
实际上,在 Stack Overflow 上,当您解决了自己的问题后,您应该将解决方案发布为答案,并将 that 标记为已接受,而不是在前面加上 '[已解决] ' 到你的问题的标题