Search code examples
phpcookiessetcookie

How to access cookie information from a webpage with a different URL


My code set a cookie as shown in below; I cannot get the cookie information from a page with a different URL... Is there something wrong in my code? Please let me know if you have any question.

zzz.php;

  setcookie ("mail", $mail, time()+3600*24*365*10);
  setcookie ("name", $name, time()+3600*24*365*10);
  setcookie ("password", $password, time()+3600*24*365*10);
  .....
  http_response_code(301);
  header("Location: ../xxx.php");

xxx.php;

echo $_COOKIE['mail'];

On xxx.php , nothihg comes up.


Solution

  • http://php.net/setcookie

    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.

    If you want the cookie to be available in the parent directory, you'll need to set the path for the cookie. Easiest way to do that is to set all your cookies to a path of /, so the cookies are available everywhere on that domain:

    setcookie ("mail", $mail, time()+3600*24*365*10, '/');
    setcookie ("name", $name, time()+3600*24*365*10, '/');
    setcookie ("password", $password, time()+3600*24*365*10, '/');