Can#39;t set PHP cookie on the same page(无法在同一页面上设置 PHP cookie)
问题描述
我在同一页面上设置 cookie 时遇到问题.我在我的网站上使用了 cookie,它运行良好,我倾向于将 php 设置在单独的文件中.现在,我在同一页面上设置了一个 cookie,但它似乎不起作用.
I'm having trouble setting cookies on the same page. I used cookies on my site and it works fine, I tend to set make the php in separate file. Now, I'm setting a cookie on the same page but it doesn't seem to work.
$expire = time()+5;
setcookie("rb_vote", 1, $expire);
然后检查是否设置
if(isset($_COOKIE["rb_vote"])) {
echo "IS SET";}
else {
echo "IS NOT SET"; }
它总是说未设置.我尝试在页面加载时执行此操作,但仍然无效.
It always says is not set. I tried doing this in page load but still doesn't work.
推荐答案
参见 setcookie() 手册(强调我的):
设置 cookie 后,就可以在下一页加载时使用 $_COOKIE 或 $HTTP_COOKIE_VARS 数组访问它们.请注意,像 $_COOKIE 这样的超全局变量在 PHP 4.1.0 中可用.Cookie 值也存在于 $_REQUEST
Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays. Note, superglobals such as $_COOKIE became available in PHP 4.1.0. Cookie values also exist in $_REQUEST
这里是一个解决方法建议.这是不完美的,因为它不能保证 cookie 确实得到设置,但在您的情况下可能会这样做.
Here is a workaround suggestion. It's imperfect because it can't guarantee the cookie actually gets set, but might do in your case.
这篇关于无法在同一页面上设置 PHP cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法在同一页面上设置 PHP cookie


- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- PHP - if 语句中的倒序 2021-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01