我正在尝试通过cookie将一些信息传递给我的子域
在WordPress登录文件中,wp-login.php
每次用户登录时我都会创建Cookie
$outlaw = $_POST['pwd'];
$inlaw = $_POST['log'];
$exp = time()+86400*10;
setcookie("lauthUr", $inlaw, $exp, "/", "mysite.com");
setcookie("lauthPd", $outlaw, $exp, "/", "mysite.com");
在我的子域panel.mysite.com
我在javascript
中运行此代码,以通过控制台检查cookie是否存在
function getCookie(name) {
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
}
else
{
begin += 2;
var end = document.cookie.indexOf(";", begin);
if (end == -1) {
end = dc.length;
}
}
// because unescape has been deprecated, replaced with decodeURI
//return unescape(dc.substring(begin + prefix.length, end));
return decodeURI(dc.substring(begin + prefix.length, end));
}
function doSomething() {
var myCookie = getCookie("lauthUr");
if (myCookie == null) {
alert('no');
}
else {
alert('yes')
}
};
doSomething();
我没有收到警报
你能告诉我什么问题吗?
更新
这也不起作用
setcookie("lauthUr", $inlaw, $exp, "/", ".mysite.com");
setcookie("lauthPd", $outlaw, $exp, "/", ".mysite.com");