Asked  7 Months ago    Answers:  5   Viewed   26 times

I'm trying to access a cookie's value (using $_COOKIE) immediately after calling the setcookie() function in PHP. When I do so, $_COOKIE['uname'] isn't set. Why?

Note, however, that $_COOKIE['uname'] is set as expected upon the next execution of the script, such as after a page refresh.

setcookie('uname', $uname, time() + 60 * 30);
echo "Cookie value: " . $_COOKIE['uname'];



$_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);  
Wednesday, March 31, 2021
answered 7 Months ago

Most likely, you are allowing access to your site via both domain names and and you have not specified the domain name for which your cookie should be set. The fifth parameter to setcookie() specifies domain, so set it to only. That will make it available to the higher domain as well.

setcookie('key', 'value', time(), '/path', '');

Via URL rewriting, you can force all users of your site to one or the other of those domains before the cookie is set, but that is merely a solution for consistency of user experience, not a way to manage cookies.

From the docs:


The domain that the cookie is available to. Setting the domain to '' will make the cookie available in the www subdomain and higher subdomains. Cookies available to a lower domain, such as '' will be available to higher subdomains, such as ''. Older browsers still implementing the deprecated ยป RFC 2109 may require a leading . to match all subdomains.

Wednesday, March 31, 2021
answered 7 Months ago

Two things I can think of:

a) localhost is not a valid domain, so cookies won't be saved for all browsers. Create yourself a HOST for "" or "" and point to (you may also need to configure apache to respond to that name - but try it first just changing the HOSTS file first)

b) In addition, your "domain" includes a scheme and a path - that might be causing problems? Set to "" (drop the "http://" and the "/tvc" parts - once you've moved away from localhost.

Wednesday, March 31, 2021
answered 7 Months ago

Setting the domain to '' or '' will make the cookie only available in the www subdomain.

If you want to make the cookie available on all subdomains of (including itself) then you'd set it to ''.

Wednesday, March 31, 2021
answered 7 Months ago

Make sure you have a domain that is known by both server and client. echo $_SERVER['HTTP_HOST'] should tell you the exact same domain that your browser has. If not, cookie will not be accepted by the browser.

Make sure your server and client time is perfectly correct. Browser will reject a cookie with a wrong datetime.

Do not write any other code and just do:

$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day 
// expiration
echo date("H:i:s d.m.Y")."<br>";
echo $_SERVER['HTTP_HOST']."<br>";

and refresh the page twice.

Also check out manual at:

Saturday, May 29, 2021
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :