stevey
stevey

Reputation: 1177

How can I pass cookie from one page to another which the two pages are not in the same directory?

I have one server but two directories for example:

  • www
    • Directory1
      • index.php
      • page1.php
      • page2.php
    • Directory2
      • index.php
      • page3.php
      • page4.php

When I want to transfer from the page index.php of Directory1 to the page index.php of Directory2, how can I pass the cookies set in the page index.php of Directory1 using PHP?

Upvotes: 0

Views: 2573

Answers (3)

Attila Szasz
Attila Szasz

Reputation: 3073

Just don't set the optional Path parameter, and then it is available to both directories.

Upvotes: 0

Ibrahim Azhar Armar
Ibrahim Azhar Armar

Reputation: 25763

according to PHP documentation

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.

$time = time() + 3600;
setcookie('foo', 'bar', $time, "/");

when you set the fourth parameter i.e path as /, it will be accessible to you on the domain level, and i guess that is what you need.

Upvotes: 1

Waleed Khan
Waleed Khan

Reputation: 11487

Use the path parameter:

setcookie("name", "value", time() + 3600, "/");

Upvotes: 2

Related Questions