carl johnson
carl johnson

Reputation: 425

Why COOKIES are not accesible from another php page?

I have two pages add_to_cart.php and index.php.
In add_to_cart.php, I have set the cookie as setcookie( "add_to_cart", $cookie_value, strtotime( '+2 days'));, Now I can access this cookie data from the same page where it was created (here add_to_cart.php) but I tried to acess this cookie by using var_dump($_COOKIE['add_to_cart']); from index.php page and an PHP NOTICE triggered -

Notice: Undefined index: add_to_cart
I have seen many solution for this problem in stackOverflow but nothing fits for me. How can I access cookies from another php pages ?

Upvotes: 0

Views: 124

Answers (1)

rjdown
rjdown

Reputation: 9227

According to the documentation, the fourth parameter of setcookie contains the path to which the cookie should apply:

The path on the server in which the cookie will be available on. If set to '/', the cookie will be available within the entire domain. If set to '/foo/', the cookie will only be available within the /foo/ directory and all sub-directories such as /foo/bar/ of domain. The default value is the current directory that the cookie is being set in.

So set it to / and you'll be able to access it from any folder (and page).

Upvotes: 1

Related Questions