setcookie not working on safari and firefox

时间:2016-07-11 19:17:50

标签: php cookies setcookie

I'm trying to set cookies on my project, but it is not working on Safari or Firefox.

I get results on Google Chrome though.

Here is my code:

 setcookie('mycookiename', 'data', time()+60*60*24*365, '/', false);
 print_r($_COOKIE['mycookiename']);

Thanks for help.

2 个答案:

答案 0 :(得分:1)

This code work properly

setcookie("mycookiename", 'data', time()+60*60*24*365, "/", "", "", TRUE);
print_r($_COOKIE['mycookiename']);

答案 1 :(得分:0)

对我来说是因为ajax和fetch。

获取不会发送Cookie,除非您设置凭据初始化选项:

fetch(url, {
    method: 'POST',
    credentials: 'same-origin',
    headers: {...},
    ...
  });

如果您正在执行跨源请求,似乎也应该设置Access-Control-Allow-Credentials: true标头。看到这个: https://stackoverflow.com/a/40255834/4718434