Get the cookie value in PHP?

前端 未结 3 609
情深已故
情深已故 2021-01-16 05:16

I create the cookie in jQuery. I can verify it in Firefox. But when I try to print the cookie or assign into another value I cant get it. I also use sessions. And I started

3条回答
  •  自闭症患者
    2021-01-16 05:55

    To set the value of a cookie in PHP, use the setcookie function:

    setcookie("a", "foobar");
    

    Doing this, however, requires that you run the code before you output to the client (and send the HTTP headers. So basically, you can't do something like this:

    Here's my cookie!

    You would need to call setcookie before you have any of your HTML.

    Also, if you set the cookie value on the client, you need to be making a request from that client to get the cookie value. If you're just running arbitrary code on the server, the client isn't sending it's cookie back up.

    Lastly, the page that the server is processing and the page that the JS is on need to be in the same domain. If they're not, the browser doesn't send up the appropriate cookie.

    Hope this helps!

提交回复
热议问题