Pregunta

I've got the following PHP code which sets a cookie called lang to the value of en.

setcookie('lang','en',time() + (86400 * 14));

This is sometimes set via a script on the root or sometimes inside a subfolder. I'm reading the value of the cookie from subfolders and the root like this:

echo $_COOKIE['lang'];

Problem i am having is that if i set the cookie from inside a subfolder, i cannot read that value. So a cookie value seems to be created for each subfolder.

Any ideas how i can resolve this? I dont want to have a cookie for each folder for the same cookie name lang

* UPDATE *

I've changed to code to make the cookie apply to /. Does this mean that the cookie will apply to all subfolders?

setcookie('lang',$selected_language,time() + (86400 * 14),'/'); 
¿Fue útil?

Solución

As you can see here http://php.net/setcookie that is the default behavior for setcookie() function.

The path on the server in which the cookie will be available on. If set to '/', the cookie will be available within the entire domain. If set to '/foo/', the cookie will only be available within the /foo/ directory and all sub-directories such as /foo/bar/ of domain. The default value is the current directory that the cookie is being set in.

You should set your path to '/' if you want your cookies to be valid for whole domain.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top