How to set cookies in PHP for a domain

I want to set a cookie via PHP. The scenario is as follows:

Domain: example.com

There is one webpage in the subdomain (my.example.com). My code is:

$value="I am looged in";
setcookie("TestCookie", $value,'','',".example.com");
echo "hello".$_COOKIE["TestCookie"];

but the result is only “hello” - the cookie is not set. Please suggest a possible solution.

Thank!

+5
source share
1 answer

The first two corrections to the actual setcookie call : Parameter 3 ( expired) must be an integer value (default value is 0); four must be set to '/'to make the cookie valid for all subdirectories; the call setcookieshould look like this:

setcookie("TestCookie", $value, 0, '/', ".example.com");

, script. , , cookie; , Cookies - , , " ". , setcookie : , cookie, , ; $_COOKIE, , , cookie - , script $_SESSION , cookie , .

+9

All Articles