javascript - 如何使用 cookie 进行重定向
问题描述
我想在同一个站点上进行两种类型的域重定向(if else 语句等):
If user has cookie:
And url subdomain is amazon then redirect to url xxxxxx
And url subdomain is ebay then redirect to url xxxx
If user has no cookie:
And url subdomain amazon then first forward to ebay subdomain and back to amazon subdomain once (currently creates a loop) and display background xxxx with href link xxxx
And url subdomain ebay then first forward to amazon subdomain and back to ebay subdomain once (currently creates a loop) and display background xxxx with href link xxxx
同步用户跨域单击以获取 href xxxxx 的新弹出窗口 - 即,如果用户通过单击另一个站点上的链接进入站点,则查看异步单击以打开另一个弹出窗口(200 秒规则)
当前站点已经具有我仍然需要的以下内容,因此由于下面的代码,cookie重定向不应设置到外部站点的重定向,但前提是用户确实是返回用户
var subdomain = window.location.hostname.split('.')[0];
if (!document.cookie == null && subdomain === "amazon") {
window.location = "http://www..com";
} else if (!document.cookie == null && subdomain === "ebay") {
window.location = "http://www.tutorialspoint.com";
} else if (document.cookie == null && subdomain === "amazon") {
var oLinksArray = [];
oLinksArray[0] = 'http://www.ebay.com';
oLinksArray[1] = 'http://www.amazon.com';
for (var x = 0; x < 2; x++) {
var openWindow = window.open(oLinksArray[x]);
setTimeout(function() {
openWindow.close();
}, 2000);
}
} else if (document.cookie == null && subdomain === "ebay") {
var oLinksArray = [];
oLinksArray[0] = 'http://www.amazon.com';
oLinksArray[1] = 'http://www.ebay.com';
for (var x = 0; x < 2; x++) {
var openWindow = window.open(oLinksArray[x]);
setTimeout(function() {
openWindow.close();
}, 2000);
}
} else {}
解决方案
您不需要通过重定向传输 cookie。Cookie
有一个名为 的属性domain
。您应该添加网站的域以授予它们访问权限。
在 Javascript 中
var cookieName = 'HelloWorld';
var cookieValue = 'HelloWorld';
var myDate = new Date();
myDate.setMonth(myDate.getMonth() + 12);
document.cookie = cookieName +"=" + cookieValue + ";expires=" + myDate
+ ";domain=firstDomain.com,nextDomain.com;path=/";
在 PHP 中
setcookie('mycookie','mydata1',time() + 2*7*24*60*60,'/','www.firstDomain.com,nextDomain.com', false);
推荐阅读
- scikit-learn - 使用 sklearn 获取每个数据点的类概率 p(c|x)
- python - 仅将“未删除”记录从 DBF 文件中读取到数据框中
- redirect - 将页面重定向到 lighttpd 中的特定 IP 和端口
- c# - 将 JSON 对象反序列化为动态对象
- java - Spring 工具:上下文初始化期间遇到异常 - 无法启动 Web 服务器;嵌套异常是
- php - 帖子不通过复选框保存
- dart - 异常不会在 Dart 中自动传播
- node.js - 在heroku中将mongodb uri设置为配置变量时出错
- python - 如何在熊猫数据框中迭代和比较字符串与手动输入?
- javascript - 脚本 AppendRow 在顶部