Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Read cookies created with setcookie()

Tags:

php

cookies

From the manual:

Cookies will not become visible until the next loading of a page that the cookie should be visible for

Meaning that a cookie created with setcookie will not be accessible in $_COOKIE until the next page load. Is there a work around for this? Is there a way to detect the cookies created with setcookie in PHP without a reload?

I can't modify the code near the setcookie call in the current codebase. So an solution like this won't work:

setcookie('test', 'my test value');
$_COOKIE['test'] = 'my test value';
like image 638
leepowers Avatar asked Nov 10 '10 19:11

leepowers


1 Answers

The _COOKIES superglobal is created as part of the script's initialization, and then is left alone by PHP. As such, any cookies you create later in the script will not magically appear in the array, as they were not present at initialization time.

You can override the built-in setcookie functionality with runkit_function_redefine(), but if you're not very careful, you can open yourself up to a world of hurt.

like image 172
Marc B Avatar answered Sep 22 '22 10:09

Marc B