在域/子域上设置 cookie

发布于 2024-12-03 14:21:43 字数 380 浏览 0 评论 0原文

大家好,StackOverflow 的好心人,我遇到了一个问题,希望你们中的一些人能够帮助我。 假设我应该在 www.webshop.com 域上创建某种 Wordpress 网上商店,用英语编写,并为该域提供一个名为“cartItems”的 cookie。好吧,没问题。

问题是我还需要制作该网站的德语版本,并且我需要区分英语和德语 cookie。

现在,我的问题是,例如,如果德国网站位于 www.webshop.com/de/ 上,我可以将德国 cookie 设置为仅适用于 /de/ 域,对吧? 但是我怎样才能设置英语 cookie 在除 /de/ 域之外的所有域中可用。

如果无法做到这一点,如果德国网站位于 de.webshop.com 上,是否可以

谢谢您的所有帮助

Hi there good people of StackOverflow, I have a problem and I was hoping that some of you out there might be able to help me.
Let's say I am supposed to create some kind of Wordpress web shop on www.webshop.com domain, written in English language, with a cookie named 'cartItems' available for that domain. Ok, not a problem.

The problem is that I also need to make a german version of the site and I need to make difference between english and german cookies.

Now, my question is, if for example the german site is on the www.webshop.com/de/ I can set a german cookie to be available only for the /de/ domain, right?
But how can I set that english cookie will be available in all the domains except the /de/ one.

If that can't be done would it be possible if the german site was on de.webshop.com

Thank you for all the help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

燃情 2024-12-10 14:21:44

我个人使用这个插件来解决这个问题:
http://wpml.org/

但如果您更喜欢免费的解决方案,这里有一篇非常好的文章:
http://codex.wordpress.org/Multilingual_WordPress

Personally I have used this plugin to solve this problem:
http://wpml.org/

But if you prefer a free solution, here is a very good article:
http://codex.wordpress.org/Multilingual_WordPress

沙与沫 2024-12-10 14:21:43

这是可能的,尽管您当然必须使用两种不同的 cookie 名称,一种用于 EN 数据,另一种用于 DE 数据。

使用不同的路径(EN 为 www.webshop.com,DE 为 www.webshop.com/de/):cookie

// this will be available everywhere, both on the english version and the german one
setcookie ('my_en_cookie_name', 'some data', time() + 24*30*3600, '/');
// this will only be visible when accessing the german website, english version user won't be able to see it
setcookie ('my_de_cookie_name', 'some other data', time() + 24*30*3600, '/de/');

的路径是向后阻塞的(只能从指定的目录或其子目录中读取,而不能从父级),因此如果您使用 /de/ 路径设置 cookie,则可以从 /de/ 或 /de/foo/ 读取它,但不能从 / 或 /bar/ 读取它。同样的,如果你设置一个路径为/的cookie,它可以在/de/或/de/foo/或/bar/上读取。


使用不同的域(英语为 www.webshop.com,德语为 de.webshop.com):

// this will be available everywhere, both on the english version and the german one
setcookie ('my_en_cookie_name', 'some data', time() + 24*30*3600, '/', '.webshop.com');
// this will only be visible when accessing the german website, english version user won't be able to see it
setcookie ('my_de_cookie_name', 'some data', time() + 24*30*3600, '/', 'de.webshop.com');

注意:在域“.webshop.com”上设置 cookie 意味着此 cookie 将被发送到每个< /strong> webshop.com 的子域,无论它是什么。确保这就是您想要的。

It is possible, although of course you have to use two different cookie name, one for the EN data and one for the DE data.

Using a different path (www.webshop.com for EN and www.webshop.com/de/ for DE):

// this will be available everywhere, both on the english version and the german one
setcookie ('my_en_cookie_name', 'some data', time() + 24*30*3600, '/');
// this will only be visible when accessing the german website, english version user won't be able to see it
setcookie ('my_de_cookie_name', 'some other data', time() + 24*30*3600, '/de/');

Path for cookie is backward blocking (it can only be read from the directory specified or from a subdirectory of it, not from a parent one), so if you set a cookie with the /de/ path, it can be read from /de/ or /de/foo/ but not from / or /bar/. In the same way if you set a cookie with a path of /, it can be read on /de/ or /de/foo/ or /bar/.


Using a different domain (www.webshop.com for EN and de.webshop.com for DE):

// this will be available everywhere, both on the english version and the german one
setcookie ('my_en_cookie_name', 'some data', time() + 24*30*3600, '/', '.webshop.com');
// this will only be visible when accessing the german website, english version user won't be able to see it
setcookie ('my_de_cookie_name', 'some data', time() + 24*30*3600, '/', 'de.webshop.com');

PAY ATTENTION: Setting a cookie on the domain ".webshop.com" means that this cookie will be sent to every subdomain of webshop.com, no matter what it is. Make sure that's what you want.

2024-12-10 14:21:43

有 3 种方法可以区分英语和德语 cookie

1) 域:
您可以使用单独的 www.和德。正如您提到的子域。对我来说,这将是最简单的

2)路径:
您可以使用该路径,但如果您的英文 cookie 设置为“/”,则仍然可以在“/de/”下访问它。因此,如果你想走这条路,你需要按照 Kerrek 的建议创建一个“/en/”路径。

3)姓名:
为德语 cookie 指定一个不同的名称

您需要执行这 3 项操作之一才能获得单独的英语和德语 cookie。我认为拥有一个单独的子域是最好的,因为无论语言如何,不仅您的 cookie 路径而且所有资产路径都将相同。

There are 3 ways you can separate the English and German cookies

1) Domain:
You could use a separate www. and de. subdomain as you mentioned. To me this would be the easiest

2) Path:
You can use the path, but if your English cookie is set for "/" it will still be accessible under "/de/". So if you want to go this route you'd need to make a "/en/" path as Kerrek suggested.

3) Name:
Give the German cookie a different name

You will need to do one of those 3 to have separate English and German cookies. I'd think that having a separate subdomain would be the best, because not just your cookie paths but all your asset paths will be the same regardless of language.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文