PHP cookie有一个刷新延迟 [英] PHP cookie has a one refresh delay

查看:179
本文介绍了PHP cookie有一个刷新延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Cookie仅在刷新时显示吗?

设置一个基本的PHP cookie,它会记住哪个颜色主题a使用。当我测试它的时候,在设置和颜色主题的显示之间有一个刷新延迟。

I'm trying to set a basic PHP cookie that will remember which color theme a uses. When I test it out however, there is a one-refresh lag between the setting and the displaying of the color theme.

澄清:我有一个选择形式将允许用户选择颜色。然后,有一个应用按钮。一旦选择了应用按钮,页面应该自动设置cookie并显示颜色。但是,当按下应用按钮时,只有cookie被设置,并且主题不显示,直到用户点击刷新按钮。有关如何解决这个问题的任何想法吗?

To clarify: I have a select-form that will allow users to choose colors. Then, there is an apply button. Once that apply button is chosen, the page should automatically set the cookie and show the color. However, when the apply button is pressed, only the cookie is set and the theme is not shown until the user hits the refresh button. Any ideas on how to solve this?

$help = $_POST["theme"];
    $color = "#" . $help;
    setcookie("TestCookie", $color, time()+60*60*24);
    $recolor = $_COOKIE["TestCookie"];
    echo '<style type="text/css"> body { background: '.$recolor.' } </style>';

其中$ _POST [theme]是选择形式的名称。非常感谢!这是: Cookie仅在刷新时显示吗?是我在说的,但是PHP,而不是C#

where $_POST["theme"] is the select-form name. Thanks a lot! this: Cookie only displayed on refresh? is what I'm talking about, but with PHP, not C#

编辑:感谢所有的回复!我设法通过设置背景颜色为用户选择同时设置cookie的同时解决这个问题。所以当用户最初选择颜色时,不是cookie设置背景。这是另一个功能。

Thanks for all the replies! I managed to work around this by setting the background color to whatever the user picked while setting the cookie at the same time. So when the user initially picks the color, it isn't the cookie setting the background. It's another function. It feels kinda cheap, but it works!

推荐答案

$ _COOKIE superglobal在您请求页面时提供cookie,这就是为什么你永远不会看到你刚刚保存的值

The $_COOKIE superglobal has the cookies available at the time you requested that page, that is why you will never see the values you just saved

在这里的手册页 http://php.net/set_cookie ,您可以看到:

On the man page here http://php.net/set_cookie you can see:


Cookie设置完成后,可以在下一页访问
加载$ _COOKIE或$ HTTP_COOKIE_VARS数组。

Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays.

这篇关于PHP cookie有一个刷新延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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