点击设置PHP cookie [英] set PHP cookie on click

查看:119
本文介绍了点击设置PHP cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我宁愿不使用JS / jQuery这个 - 但我似乎不能让这个工作。



我有一个链接< a href =?hideupdates = hide>隐藏更新< / a> 我尝试设置Cookie。

  if($ _ GET ['hideupdates'] =='hide'){
setcookie(HideUpdates,hide,time 60 * 60 * 24 * 5,/,$ vars-> networkSite);
}

it工作,但我必须单击链接两次。 site.com 可以 var_dump()

site.com 中选择






该cookie,它出现 NULL



现在我点击链接, strong> site.com?hideupdates=hide ,且Cookie仍然出现 NULL



,当我再次点击该链接时,从 site.com?hideupdates=hide 开始 - c $ $ b
$ b

我错过了什么?或者,我是否可以使用JS / jQuery?

解决方案

setcookie 不影响当前请求。为此,您还需要手动设置相关的 $ _ COOKIE 变量:

  setcookie(HideUpdates,$ _ COOKIE ['HideUpdates'] =hide,time()+ 60 * 60 * 24 * 5,/,$ vars-> networkSite); 


So I'd rather not use JS/jQuery for this - but I can't seem to get this to work.

I've got a link <a href="?hideupdates=hide">Hide Updates</a> which I'm trying to set a cookie with.

if($_GET['hideupdates'] == 'hide'){
    setcookie("HideUpdates", "hide", time()+60*60*24*5, "/", $vars->networkSite);
}

it "works", but I have to click the link twice.


from "site.com" I can var_dump() the cookie and it comes up NULL

Now I click the link and go to "site.com?hideupdates=hide" and the cookie still comes up NULL

However, from "site.com?hideupdates=hide" when I click the link again - THEN the cookie comes back hide.

Am I missing something? Or do I 'have' to use JS/jQuery for this?

解决方案

setcookie does not affect the current request. To do that, you also need to manually set the relevant $_COOKIE variable:

setcookie("HideUpdates",$_COOKIE['HideUpdates'] = "hide", time()+60*60*24*5, "/", $vars->networkSite);

这篇关于点击设置PHP cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆