如果两个页面不在同一目录中,我如何将cookie从一个页面传递到另一个页面


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

我有一个服务器,但有两个目录,例如:

  • www
    • 目录1
      • index.php
      • page1.php
      • page2.php
    • 目录2
      • index.php
      • page3.php
      • page4.php

当我想从Directory1index.php页面转移到Directory2index.php页面时,如何使用PHP传递Directory1index.php页面中设置的cookie?

使用path参数:

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

根据PHP文档

cookie可用的服务器上的路径。如果如果设置为"/",cookie将在整个域中可用。如果设置为"/foo/",cookie将仅在/foo中可用/目录和域的所有子目录,如/foo/bar/of。这个默认值是设置cookie的当前目录in.

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

当您将第四个参数(即path)设置为/时,您可以在域级别访问它,我想这正是您所需要的。

只要不设置可选的Path参数,它就对两个目录都可用。