Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set PHP cookie on click

Tags:

php

cookies

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?

like image 685
Xhynk Avatar asked Dec 15 '22 15:12

Xhynk


1 Answers

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);
like image 65
Niet the Dark Absol Avatar answered Dec 31 '22 10:12

Niet the Dark Absol