开发者

Setting cookies on domain/subdomain

开发者 https://www.devze.com 2023-04-03 21:27 出处:网络
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.

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.co开发者_JAVA百科m

Thank you for all the help


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.


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消