Cookie不在子域上识别

Cookie不在子域上识别

本文介绍了Cookie不在子域上识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个域和一个子域,我需要识别主域设置的cookie。在www.mydomain.com上我使用Javascript设置cookie,如下所示:

I have a domain and a subdomain which I need both to recognise a cookie set by the main domain. On www.mydomain.com I set the cookie with Javascript like so:

var d = new Date();
d.setDate(d.getDate() + 30);
var c = "all; expires=" + d.toUTCString() + "; path=/;domain=mydomain.com";
document.cookie = "cookies=" + c;

在PHP中我使用简单的 if(isset($ _ COOKIE [' cookies']))... 这适用于www.mydomain.com,但它不适用于sub.mydomain.com。

In the PHP I use a simple if (isset($_COOKIE['cookies'])) ... This works on www.mydomain.com but it doesn't work on sub.mydomain.com.

有什么建议吗?我是否必须以不同的方式在PHP中调用cookie?

Any suggestions? Do I have to call the cookie within PHP differently?

推荐答案

var d = new Date();
d.setDate(d.getDate() + 30);
var c = "all; expires=" + d.toUTCString() + "; path=/;domain=.mydomain.com";
document.cookie = "cookies=" + c;

这篇关于Cookie不在子域上识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 17:46