Cookie not setting for all directories

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Cookie not setting for all directories

Post by andym01480 »

I have a simple remember my login used in a login script for /admin/ directory.

Code: Select all

if($_POST['remember']=='y') setcookie('remember',$data['userlevel'],time()+60*60*24*30,'/');
it is not holding for another directory which relative to the script setting it is ../fpdf/

What have I done wrong?
koen.h
Forum Contributor
Posts: 268
Joined: Sat May 03, 2008 8:43 am

Re: Cookie not setting for all directories

Post by koen.h »

andym01480 wrote:I have a simple remember my login used in a login script for /admin/ directory.

Code: Select all

if($_POST['remember']=='y') setcookie('remember',$data['userlevel'],time()+60*60*24*30,'/');
it is not holding for another directory which relative to the script setting it is ../fpdf/

What have I done wrong?
If the path for the cookie is 'dir/subdirA', your cookie won't be available for 'dir/subdirB' (which is '../fpdf/').
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Cookie not setting for all directories

Post by andym01480 »

That's what I am experiencing. But the manual says...
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.
So is there a bug in the manual or php?
koen.h
Forum Contributor
Posts: 268
Joined: Sat May 03, 2008 8:43 am

Re: Cookie not setting for all directories

Post by koen.h »

andym01480 wrote:That's what I am experiencing. But the manual says...
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.
So is there a bug in the manual or php?
You got me. I now see that the path you have set in the cookie is root. So the cookie should be available in the whole domain. But aren't we mixing server directories and domain paths?
koen.h
Forum Contributor
Posts: 268
Joined: Sat May 03, 2008 8:43 am

Re: Cookie not setting for all directories

Post by koen.h »

andym01480 wrote:That's what I am experiencing. But the manual says...
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.
So is there a bug in the manual or php?
You got me. I now see that the path you have set in the cookie is root. So the cookie should be available in the whole domain. But aren't we mixing server directories and domain paths?
Post Reply