gpt4 book ai didi

javascript - 直到第二次刷新才设置 Cookie

转载 作者:可可西里 更新时间:2023-10-31 23:31:12 27 4
gpt4 key购买 nike

我在第 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 而不是 session 。如果这不是正确的做法,请纠正我。

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

<?php
$itemcookie = $_POST['item'];
$detsinationcookie = $_POST['destination'];

setcookie("itemcookie", $itemcookie, strtotime('+30 days'));
setcookie("destinationcookie", $detsinationcookie, strtotime('+30 days'));
?>

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

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

如何在第 2 页立即获取可用的 cookie 数据?

最佳答案

你不能。

如果您检查 manual :

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

这意味着您的 cookie 在您设置它们的页面/脚本上将不可用。

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

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

关于javascript - 直到第二次刷新才设置 Cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21920162/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com