最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Setting cookies on domainsubdomain - Stack Overflow

programmeradmin1浏览0评论

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 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/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

Thank you for all the help

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. 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./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.

Thank you for all the help

Share Improve this question asked Sep 7, 2011 at 21:44 Slavenko MiljicSlavenko Miljic 3,8562 gold badges24 silver badges37 bronze badges 6
  • 2 /de/ is not a domain. Why not store a language flag inside the cookie, or even inside the server-side backend? – Kerrek SB Commented Sep 7, 2011 at 21:46
  • Yeah i understand that I just didn't know how to express myself properly. I cant use the same cookie because of the different currencies and whatnot, I guess that it could be done with some kind of conversion but I'm not that good :) – Slavenko Miljic Commented Sep 7, 2011 at 21:53
  • 2 It's called a "path", and given that that very same term is used in the specification of cookies, it's a little worrying that you should be unfamiliar with it while trying to build a money transacting application with this very technology. Anyway, cookie paths may not solve your problem, but I somehow wonder if this is good design at all; I have a feeling that all this should be done server-side, and your cookie should really only track the session ID... – Kerrek SB Commented Sep 7, 2011 at 21:56
  • I'm not doing much of the programming regarding the webshop, it's a wordpress theme that is already set up. I need to separate those two shops because the prices are different in euros and in dollars since the theme doesn't support different currencies. Oh and thank you for trying to help – Slavenko Miljic Commented Sep 7, 2011 at 22:05
  • 2 Well, if the web application is fixed, can't you put one under /en/ and one under /de/ and run them in parallel, rather than having one nested under the other? – Kerrek SB Commented Sep 7, 2011 at 22:07
 |  Show 1 more ment

3 Answers 3

Reset to default 5

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. for EN and www.webshop./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. for EN and de.webshop. 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.');
// 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.');

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

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.

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

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

发布评论

评论列表(0)

  1. 暂无评论