PHP Cookie在localhost上运行良好,但它不能在活动服务器上工作 [英] PHP Cookies works well on localhost, but it's not working on live server

查看:138
本文介绍了PHP Cookie在localhost上运行良好,但它不能在活动服务器上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


注意:这个问题已经解决了,
finally我发现它不是cookie
问题,问题是在
unserialize()函数。作为
函数的参数的序列化的
cookie首先必须是stripslash-ed

Note: This issue is already solved, finally I found that it's not cookies problem, the problem is on unserialize() function. The serialized cookie which being the parameter of that function must be stripslash-ed first.

我有一个问题在这里关于PHP Cookie。我使用PHP Cookie来保存用户偏好。我测试了我的代码在我的本地机器(localhost使用XAMPP)。一切都很好,包括饼干。但是当我上传到活动服务器时,cookies根本不工作。看来setcookie()函数不写入cookie值。我已经测试通过回显在我的本地主机和我的活动服务器上的cookie值。 $ _COOKIE [] localhost上的值,但不显示在活动服务器中的值。

Hi there, I have a problem here about PHP Cookies. I'm using PHP Cookies to save user preferences. I've tested my code on my local machine (localhost using XAMPP). Everything's works very well, including the cookies. But when I uploaded it to the live server, the cookies not working at all. It seems that the setcookie() function do not write the cookie value. I've tested by echo-ing the cookie value both on my localhost and on my live server. $_COOKIE[] value on localhost is showing but not with the one in the live server.

我想也许它与$ expire时区相关,就像这篇文章中的那样。 http://anupraj.com.np/index.php/php-cookies-not-working-php -cookie-tutorial-and-scirpt / 14 。但是后来我意识到我已经设置了cookie在1个月内过期,不仅仅是在那个博客上的一个小时。

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 . 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. So I think that's not the case.

This is the content of setting.php

code><?php
$ defaultSettings ['default_post_to'] ='both';
$ defaultSettings ['timesince_style'] ='simplified';
...
$ 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']);
?>

<?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的内容

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

我在任何地方都搜索过(Google,stackoverflow,在twiiter / FB上问朋友)解决方案

I've search about it everywhere (Google, stackoverflow, asking friends on twiiter/FB) still no solutions

我希望一些机构可以给我的解决方案
感谢:)

I hope some body could give me the solution here Thanks :)

推荐答案

您的服务器时间可能不正确,因此Cookeis不在服务器上工作。

Probably your server time is not correct therefore Cookeis are not working on server.

尝试:

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

将到期日设为零将解决您的问题。或更新您的服务器时间。

Setting expiration to zero will fix your issue in this case. or update your server time.

这篇关于PHP Cookie在localhost上运行良好,但它不能在活动服务器上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆