lar*_*rpo 6 javascript php cookies
我在第 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>
Run Code Online (Sandbox Code Playgroud)
我希望以持久的方式保存此信息,以便即使用户随后登录(在本例中为 joomla),cookie 数据也是持久的并且可以被调用。这就是为什么我在这种情况下使用 cookie 而不是会话。如果这不是正确的方法,请纠正我。
我有一些代码来设置和检索第 2 页上的 cookie:
<?php
$itemcookie = $_POST['item'];
$detsinationcookie = $_POST['destination'];
setcookie("itemcookie", $itemcookie, strtotime('+30 days'));
setcookie("destinationcookie", $detsinationcookie, strtotime('+30 days'));
?>
Run Code Online (Sandbox Code Playgroud)
但是,当表单提交后加载时,cookie 数据不会出现在第二页上。如果我刷新第二页,数据会出现在正确的位置,即我用例如调用它的位置
<?php
echo $_COOKIE["itemcookie"];
?>
Run Code Online (Sandbox Code Playgroud)
如何立即在第2页获取可用的cookie数据?
你不能。
如果你检查手册:
Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays.
^^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)
这意味着您的 cookie 在您设置它们的页面/脚本上将不可用。
您可以使用另一个变量来显示该值,例如:
$itemcookie_value = isset($_POST['item']) ? $_POST['item'] : $_COOKIE["itemcookie"];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8038 次 |
| 最近记录: |