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
基础教程推荐
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何替换eregi() 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
