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'];

 Answers

51

$_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
 
Slinky
answered 7 Months ago
82

Most likely, you are allowing access to your site via both domain names example.com and www.example.com 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 example.com only. That will make it available to the higher domain www.example.com as well.

setcookie('key', 'value', time(), '/path', 'example.com');

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:

domain

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

Wednesday, March 31, 2021
 
Crontab
answered 7 Months ago
16

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 "my.dev.server" or "localhost.dev" and point to 127.0.0.1 (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 "localhost.dev" (drop the "http://" and the "/tvc" parts - once you've moved away from localhost.

Wednesday, March 31, 2021
 
hnkk
answered 7 Months ago
38

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

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

Wednesday, March 31, 2021
 
PeanutsMcgee
answered 7 Months ago
76

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:

<?php
$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>";
var_dump($_COOKIE);
?>

and refresh the page twice.

Also check out manual at: https://www.php.net/manual/en/features.cookies.php

Saturday, May 29, 2021
 
mattltm
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 :