我在同一页面上设置 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
在 SELECT(MYSQL/PHP) 中加入 2 个表Joining 2 tables in SELECT(MYSQL/PHP)(在 SELECT(MYSQL/PHP) 中加入 2 个表)
如何使<option selected=“selected">由How to make lt;option selected=quot;selectedquot;gt; set by MySQL and PHP?(如何使lt;option selected=“selectedgt;由 MySQL 和 PHP 设置?)
使用 PHP 中的数组自动填充选择框Auto populate a select box using an array in PHP(使用 PHP 中的数组自动填充选择框)
PHP SQL SELECT where like search item with multiple wordsPHP SQL SELECT where like search item with multiple words(PHP SQL SELECT where like search item with multiple words)
json_encode 从 MSSQL-SELECT 产生 JSON_ERROR_UTF8json_encode produce JSON_ERROR_UTF8 from MSSQL-SELECT(json_encode 从 MSSQL-SELECT 产生 JSON_ERROR_UTF8)
MySQL ORDER BY rand(),名称 ASCMySQL ORDER BY rand(), name ASC(MySQL ORDER BY rand(),名称 ASC)