Cookies not working on different pages

后端 未结 9 1643
长发绾君心
长发绾君心 2020-12-03 17:19

Ok I have a cookie set, and I can clearly see it if I go to private data in Firefox... ok so when I echo it on one page in a certain directory it works, (www.example.com/dir

相关标签:
9条回答
  • 2020-12-03 18:04
    setcookie("Cookie_name", "Cookie_Value", time()+3600 , '/' );
    

    fourth parameter ('/') will make your cookies accessible to pages in parent directories.

    0 讨论(0)
  • 2020-12-03 18:11

    Which directory are you in when the cookie gets set?

    From the PHP manual on setcookie(), emphasis mine:

    Path

    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.

    0 讨论(0)
  • 2020-12-03 18:15

    Cookies Must Be Set Before Page Output !!! Since cookies are sent by the script to the browser in the HTTP headers, before your page is sent, they must be set before you even send a single line of HTML or any other page output. The moment you send any sort of output, you are signalling the end of the HTTP headers. When that happens, you can no longer set any cookie. If you try, the setcookie() function will return FALSE, and the cookie will not be sent.

    0 讨论(0)
提交回复
热议问题