为使用多个域名的Django站点设置默认Cookie域 [英] Setting default cookie domain for Django site with multiple domain names

查看:888
本文介绍了为使用多个域名的Django站点设置默认Cookie域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的Cookie设置一个特定的Cookie域,因为这可能解决我们的网站似乎与IE8的一些问题。 Django似乎有一个名为SESSION_COOKIE_DOMAIN的设置,可以设置为获取此设置。但问题是,我们的网站包含多个具有替代域名的子网站。所以我的问题是,我该如何管理?我希望每个域都有一个标准的Cookie域,因为我担心像IE8这样的浏览器会拒绝不是来自同一域名的cookie(更快)。



我实际想要做的是使django存储cookie的domain1当我访问domain1.com etcetera为其他域。我认为它应该像存储cookie时使用当前客户端域一样简单。我怀疑django提供这样的功能没有修改...也许我可以构建一个中间件类,将全局设置更改为当前域..



更新



这个问题和答案帮助我:
根据多个站点的请求动态更改Django设置变量
感谢您的帮助:)

解决方案

无法存储或检索其他域名的Cookie。换句话说,如果我在yahoo.com,我不能得到google.com的cookie。但是,foo.yahoo.com和bar.yahoo.com都可以检索保存在.yahoo.com的Cookie。



如果您正在运行包含多个子网站的网站,他们都共享相同的基本域(即site1.domain.com,site2.domain.com等),您应该使用该域 SESSION_COOKIE_DOMAIN 。但是如果他们有不同的域,他们基本上不可能共享cookie,而不使用其他方法获取cookie。例如,您可以包含指向中央网站的图片或脚本,该网站可以存储和检索通过JavaScript在页面的其余部分提供的Cookie。



如果您必须保留这些备用域名,则可以始终将Web服务器设置为从这些备用域名直接重定向到共享标准域。这很容易用mod_rewrite。


I would like to set a specific cookie domain for my cookies, because this might solve some issues our site seems to have with IE8. Django seems to have a setting called SESSION_COOKIE_DOMAIN which can be set to obtain this. The problem however is that our site contains multiple subsites which have alternative domain names. So my question is, how can I manage this? I would like to have a standard cookie domain per domain, because I fear browsers like IE8 will reject cookies which aren't from the same domain (quicker).

I will do research myself, but I wondered if anyone perhaps has experience.

Update:

What I actually want to do is to make django store cookies for domain1 when I visit domain1.com etcetera for the other domains. I think it should be as easy as to use the current client domain when storing cookies. I doubt however that django offers such functionality without modification... Maybe I could build a middleware class that changes the global setting to the current domain..

Update:

This question and answer helped me out: Changing Django settings variable dynamically based on request for multiple site Thanks for help :)

解决方案

Cookies can't be stored or retrieved for other domain names. In other words, if I am at yahoo.com I can't get the cookie for google.com. However, foo.yahoo.com and bar.yahoo.com can both retrieve cookies saved at .yahoo.com.

If you are running a website with multiple subsites, if they all share the same basic domain (i.e. site1.domain.com, site2.domain.com, etc) you should use that domain for SESSION_COOKIE_DOMAIN. But if they have different domains, it's basically impossible for them to share cookies without using some other method of getting the cookies. You can, for example, include images or scripts that point to a central site, and that site can store and retrieve the cookies, which are made available to the rest of the page via JavaScript.

If you must keep these alternate domain names, you can always set your web server to redirect immediately from these alternate domain names to the shared standard domain. This is easy to do with mod_rewrite.

这篇关于为使用多个域名的Django站点设置默认Cookie域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆