直到第二次刷新才设置Cookie [英] Cookie not set until a second refresh

查看:173
本文介绍了直到第二次刷新才设置Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在第1页上有一个表单:

I have a form on page 1:

<form method="post" action="request-form">
<input type="text" id="amzQry" name="item" placeholder="What do you need?" autocomplete="on">
<input id="autocomplete" name="destination" placeholder="where? (e.g. Buenos Aires)" onfocus="geolocate()" type="text" required="" aria-required="true" autocomplete="off">
<button type="submit" value="" >submit</button>
 </form>

我希望以持久的方式保存此信息,以便即使用户随后登录( (在这种情况下为joomla)Cookie数据是持久性的,可以调用。这就是为什么我在这种情况下使用cookie而不是会话的原因。如果这不是正确的方法,请纠正我。

I want this information to be held in a persistent way so that even if a user subsequently logs in (to joomla in this case) the cookie data is persistent and can be called. That is why i have used cookies rather than sessions in this case. Correct me if this is not the right way of doing this.

我有一些代码可以设置和检索第2页上的cookie:

I have some code to set and retrieve the cookie on page 2:

<?php
$itemcookie = $_POST['item'];
$detsinationcookie = $_POST['destination'];
setcookie( "itemcookie", $itemcookie, strtotime( '+30 days' ) );
setcookie( "destinationcookie", $detsinationcookie, strtotime( '+30 days' ) );
?>

但是在表单提交后加载时,cookie数据没有出现在第二页上。如果刷新第二页,数据将显示在正确的位置,即我用例如

But the cookie data is not appearing on the second page when it loads after form submit. If I refresh the second page the data appears in the right places, i.e. where I have called it with e.g.

<?php echo $_COOKIE["itemcookie"]; ?>

如何在第2页上立即获取cookie数据?

How to get the cookie data available immediately on page 2?

推荐答案

您不能。

如果选中手册

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

这意味着您

您可以使用另一个变量来显示值,例如:

You could use another variable to show the value though, for example like:

$itemcookie_value = isset($_POST['item']) ? $_POST['item'] : $_COOKIE["itemcookie"];

这篇关于直到第二次刷新才设置Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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