简体   繁体   English

PHP Cookies 在本地主机上运行良好,但在实时服务器上不起作用

[英]PHP Cookies works well on localhost, but it's not working on live server

Note: This issue is already solved, finally I found that it's not cookies problem, the problem is on unserialize() function.注意:这个问题已经解决了,最后我发现不是cookies的问题,问题出在unserialize()函数上。 The serialized cookie which being the parameter of that function must be stripslash-ed first.作为该函数参数的序列化 cookie 必须首先被带斜线。

Hi there, I have a problem here about PHP Cookies.您好,我有一个关于 PHP Cookies 的问题。 I'm using PHP Cookies to save user preferences.我正在使用 PHP Cookies 来保存用户首选项。 I've tested my code on my local machine (localhost using XAMPP).我已经在本地机器(使用 XAMPP 的本地主机)上测试了我的代码。 Everything's works very well, including the cookies.一切都很好,包括饼干。 But when I uploaded it to the live server, the cookies not working at all.但是当我将它上传到实时服务器时,cookie 根本不起作用。 It seems that the setcookie() function do not write the cookie value.似乎 setcookie() 函数没有写入 cookie 值。 I've tested by echo-ing the cookie value both on my localhost and on my live server.我已经通过在我的本地主机和我的实时服务器上回显 cookie 值进行了测试。 $_COOKIE[] value on localhost is showing but not with the one in the live server.本地主机上的 $_COOKIE[] 值显示,但与实时服务器中的值不同。

I thought maybe it's related to the $expire time zone like the one's in this post http://anupraj.com.np/index.php/php-cookies-not-working-php-cookie-tutorial-and-scirpt/14 .我想这可能与 $expire 时区有关,就像这篇文章中的那样http://anupraj.com.np/index.php/php-cookies-not-working-php-cookie-tutorial-and-scirpt/14 . But then I realized that I've set the cookies to expire in 1 month, not only in one hour like on that blog post.但后来我意识到我已经将 cookie 设置为在 1 个月后过期,而不是像那篇博文那样只在 1 小时内过期。 So I think that's not the case.所以我认为事实并非如此。

This is the content of setting.php这是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']);
?>

And this is content of index.php这是 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 ){
        ...
    }
}
?>

I've search about it everywhere (Google, stackoverflow, asking friends on twiiter/FB) still no solutions我到处搜索它(谷歌,stackoverflow,在 twiiter/FB 上询问朋友)仍然没有解决方案

I hope some body could give me the solution here Thanks :)我希望有人可以在这里给我解决方案谢谢:)

While applying solutions we get forgot the basic of Cookies.在应用解决方案时,我们忘记了 Cookie 的基础知识。

Cookies are like headers. Cookie 就像标题。 Like the headers, it should be sent before any output generates.与标头一样,它应该在任何输出生成之前发送。 then only it sets successfully.那么只有它设置成功。 I have struggled a lot for this problem but when i went through the basics this problem got solved quickly.我为这个问题苦苦挣扎,但是当我完成基础知识时,这个问题很快就得到了解决。

this syntax will be enough to solve this problem...这个语法足以解决这个问题......

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

Look at both path and domain parameters for the setcookie function.查看 setcookie 函数的路径和域参数。 Reference: setcookie @ PHP docs http://php.net/manual/en/function.setcookie.php参考:setcookie @ PHP 文档http://php.net/manual/en/function.setcookie.php

Try this to set your cookie:试试这个来设置你的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
);

Good luck!祝你好运!

Probably your server time is not correct therefore Cookeis are not working on server.可能您的服务器时间不正确,因此 Cookeis 无法在服务器上工作。

Try this:尝试这个:

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

Setting expiration to zero will fix your issue in this case.在这种情况下,将到期时间设置为零将解决您的问题。 or update your server time.或更新您的服务器时间。

Try this:尝试这个:

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

Also, could it be that serialize($defaultSettings) result is too large?另外,是不是serialize($defaultSettings)结果太大了?

Try exit() after the Location-header.在 Location-header 之后尝试exit()

A Location-header does not prevent a PHP-script from executing further instructions, maybe there is something executed after the header that causes the misbehaviour.位置标头不会阻止 PHP 脚本执行进一步的指令,也许在标头之后执行的某些内容会导致错误行为。

Only initialize the ob_start() method before setcookie().只在 setcookie() 之前初始化 ob_start() 方法。 most of the developer ob_start() method include in config file.大多数开发人员 ob_start() 方法都包含在配置文件中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Magento模块在localhost上运行良好,但在实时服务器上运行不正常 - Magento module works well on localhost but not on live server php 代码在 xampp 本地主机上运行良好,但在实时服务器上运行不正常 - php code works fine on xampp localhost but not working on live server PHP json_decode在实时服务器上返回null,但在localhost上运行良好 - PHP json_decode returns null on live server but works well on localhost PHP套接字服务器在本地主机上工作,但在实时服务器上不工作 - php socket server works on localhost but not on live server php: @fopen 在 localhost 中工作,但在实时服务器中无效 - php: @fopen works in localhost but not in live server php header()在本地主机上工作,但在实时服务器上不工作? - php header() working in localhost but not working on live server? 在我的本地主机(PHP 5.3.4)上运行的代码在实时服务器(PHP 5.2.17)上的旧PHP环境下不起作用 - Code that works on my localhost (PHP 5.3.4) is not working on older PHP environment on live server (PHP 5.2.17) Ajax请求php在localhost上工作,但不在实时服务器上 - Ajax request to php working on localhost but not on live server PHP标头可在localhost上运行,但不能在实时服务器上运行 - PHP header working on localhost but not on live server php套接字服务器仅在localhost上有效,而在实时服务器上不可用 - php socket server works only on localhost but not live server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM