Accessing $_Cookie Immediately After Setcookie()

Accessing $_COOKIE immediately after setcookie()

$_COOKIE is set when the page loads, due to the stateless nature of the web. If you want immediate access, you can set $_COOKIE['uname'] yourself or use an intermediate variable.

For example:

if (isset($_COOKIE['uname'])) {
// get data from cookie for local use
$uname = $_COOKIE['uname'];
}
else {
// set cookie, local $uname already set
setcookie('uname', $uname, time() + 1800);
}

Accessing cookie immediately after setting it

This is simply how cookies work:

Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays. (setcookie manual page)

This actually makes a great deal of sense. Look at the manual page for $_COOKIE:

An associative array of variables passed to the current script via HTTP Cookies.

The value wasn't passed to the current script via HTTP Cookies, so it isn't in the $_COOKIE array.

setcookie() sets $_COOKIE to zero at a first access to the page

It doesn't. It defaults to NULL, because it hasn't been set.

setcookie merely adds the Set-Cookie header to the list of headers to be sent to the browser. It is a convenience shortcut for header("Set-Cookie: ..."); that does the formatting for you. It does not modify the $_COOKIE superglobal.

You can, of course, do it yourself:

function updatecookie($name,$val,$exp=0,$path="/",$domain="",$secure=false,$httponly=false) {
$ret = setcookie($name,$val,$exp,$path,$domain,$secure,$httponly);
if( $ret) $_COOKIE[$name] = $val;
return $ret;
}

How can I access cookies immediately?

I would recommend using normal PHP variables. At the time you set a cookie in your PHP script, you'll have to create another PHP variable containing the same value.

Example:

<?php
if (!empty($_POST['username'])){
set_cookie("username",$_POST['username']);
$username=$_POST['username'];
}
?>

At the time you need to read out the variable:

<?php if(isset($_COOKIE['username']) OR isset($username)){ ?>

<script type="text/javascript">
jQuery(document).ready(function($){
$('a.down').show().css("display", "block");
});
</script>

<?php } else { include('login.php'); } ?>

Get cookie fails immediately after set cookie in ASP.NET Core

Unfortunately in .NET Core the cookie is NOT immediately available in the HttpRequest.Cookies collection. In this respect the behavior is different than for Framework as confirmed in ASP.NET Core issue 24442.

I had hoped to use a Cookie to store the culture setting as determined in middleware because I suspect storing it Thread.CurrentThread.CurrentUICulture will not work in async methods as there is no guarantee that the thread will be the same as one used for the setting made by my middleware. Can anyone suggest a work-around?

PHP – setcookie() not working

PHP superglobals are populated at script start-up time, and then are NOT modified or touched by PHP again for the life of the script. That means $_COOKIE represents the cookies that were sent to the server in the http request that fired up the script. It will NOT show any cookies you've added/changed/deleted during the life of the script. Those changes will only show up on the NEXT request.

The only exception to this is $_SESSION, which is populated when you call session_start().

If you need those values to be in $_COOKIE immediately, you'll have to add them manually, e.g.

setcookie('cookie', $value, ....);
$_COOKIE['cookie'] = $value;


Related Topics



Leave a reply



Submit