PHP Can't read cookies?

前端 未结 4 1455
轮回少年
轮回少年 2021-01-22 12:03

I like to use PHP to see if a cookie PHPSID27258STATUS is present with the value COMPLETE en if so do stuff.

In google chrome (in Options) I ca

相关标签:
4条回答
  • 2021-01-22 12:09

    You mention that the cookie is set by a program running in its own sub-directory. You don't mention whether the cookie itself is set to be in that sub-directory, but I suspect this is where your problem is.

    If a cookie is set to a path, then it will only be accessible to pages within that path. This behaviour is described in the PHP setcookie() manual page.

    When you're setting cookies from a page within a sub-directory, then in order for the cookie to be accessible to the whole site, setcookie() needs to be called with the optional path parameter set to "/".

    You state that the cookie is being set by LimeSurvey. I don't know this software, but you should be able to look at the source and see whether it's using the path parameter when it sets the cookie. If not, your best option would be to modify it so that it does. Then the cookie will be accessible to the whole site. (It would be quite understandable if the LineSurvey developers had chosen not to set it for the whole site, because it would allow the software to be run as a more isolated entity from anything else on the site).

    0 讨论(0)
  • 2021-01-22 12:14

    You can only read cookies which belong to the same domain as the reading script. For instance if the cookie PHPSID27258STATUS was set by domain xyz.com, you can not read it using a script on abc.com. So make sure the domain of your desired cookie is the same. Also show us the code part where you are setting your cookie.

    Edit:

    setcookie($cookiename, "COMPLETE", (time() + 31536000) , '/');
    
    0 讨论(0)
  • 2021-01-22 12:17

    You cannot access a cookie immediately after you set it. At least last time it was like that. Make sure you do not have this issue. If it is not may be you have a problem setting the cookie, and in that case please post that part as well.

    0 讨论(0)
  • 2021-01-22 12:20

    Try setting the cookie with this code:

    setcookie("cookiename", "cookievalue", time() + 31536000, "/");
    

    This makes the cookie available to the whole domain, I recently encountered the same issue and when I tried this, it made it work, the reason is, your browser stores cookies for both domain.com and www.domain.com so you never know which your setting and getting from, it's good practice to set the domain even if you don't have this problem.

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