【问题标题】:PHP Cookies works well on localhost, but it's not working on live serverPHP Cookies 在本地主机上运行良好,但不能在实时服务器上运行
【发布时间】: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 标记为已接受,而不是在前面加上 '[已解决] ' 到你的问题的标题

标签: php cookies localhost


【解决方案1】:

试试这个:

setcookie("settings", serialize($defaultSettings), time()+3600*24*30, '/'); // added path

还有,会不会是serialize($defaultSettings)的结果太大了?

【讨论】:

  • 嘿,谢谢你的线索。我发现问题似乎与 serialize()/unserialize() 函数有关。 serialize 函数确实有效,但 unserialize() 函数似乎什么也没返回。真的是因为太大了吗?
  • Cookie 大小通常限制为 4 KB
【解决方案2】:

查看 setcookie 函数的路径和域参数。 参考:setcookie@PHP 文档http://php.net/manual/en/function.setcookie.php

试试这个来设置你的 cookie:

if ($on_localhost) { // change this
    $domain = '.localhost';
} else {
    $domain = '.webhoster.com'; // change this
}
setcookie(
    'settings',
    serialize($defaultSettings),
    time()+3600*24*30,
    '/',          // this is the path
    $domain       // this is the domain
);

祝你好运!

【讨论】:

    【解决方案3】:

    在 Location-header 之后尝试exit()

    Location-header 不会阻止 PHP 脚本执行进一步的指令,可能在 header 之后执行的某些内容会导致错误行为。

    【讨论】:

      【解决方案4】:

      可能您的服务器时间不正确,因此 Cookeis 无法在服务器上运行。

      试试这个:

      setcookie("settings", serialize($defaultSettings), 0);
      

      在这种情况下,将过期设置为零将解决您的问题。或更新您的服务器时间。

      【讨论】:

        【解决方案5】:

        在应用解决方案时,我们忘记了 Cookie 的基础知识。

        Cookie 就像标题。与标头一样,它应该在任何输出生成之前发送。那么只有它设置成功。我为这个问题做了很多努力,但是当我完成基础知识后,这个问题很快就解决了。

        这个语法足以解决这个问题……

        setcookie(
        'settings',
        serialize($defaultSettings),
        time()+3600*24*30,
        '/'          // this is the path
        );
        

        【讨论】:

          【解决方案6】:

          只在 setcookie() 之前初始化 ob_start() 方法。大多数开发者 ob_start() 方法都包含在配置文件中。

          【讨论】:

            猜你喜欢
            • 2020-02-25
            • 1970-01-01
            • 2018-03-17
            • 1970-01-01
            • 2011-03-21
            • 1970-01-01
            • 2014-11-29
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多