syntax for COOKIE path

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
batcloud
Forum Newbie
Posts: 15
Joined: Sun Jun 10, 2007 3:04 pm

syntax for COOKIE path

Post by batcloud »

I set a cookie in http://sometwebsitename.com/newd/ like this:
setcookie("username", "jason", time()+36000, "/newd/");
I would like to use the cookie value while running a script in another folder http://sometwebsitename.com/checkcookie.php and not from http://sometwebsitename.com/newd/ which is where the cookie is actually set.
Lets say I use: echo $_COOKIE["username"];
It doesn't work because the cookie is set in /newd/ which I understand.
So, how I do I specifiy the path that needs to be used to check for the cookie value? I tried variations like:
echo $_COOKIE["/newd/username"];
echo $_COOKIE["../newd/username"];
echo $_COOKIE["http://sometwebsitename.com/newd/username"]; etc
but no luck.
What's the syntaxt for putting in the path name?
Would greatly appreciate any help. I have been breaking my head over this for 2 days. I am a PHP newb.
Thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

http://de2.php.net/setcookie wrote:path
[...]
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 want it to be available within the whole domain.
batcloud
Forum Newbie
Posts: 15
Joined: Sun Jun 10, 2007 3:04 pm

Post by batcloud »

Thanks Volka.
However, I am unable to set the cookie to be available for the whole domain. The cookie is being set by another application, and I have tried hacking that application to do so- it works, but has other unintended consequences and breaks that application. Should have mentioned that in my original post :(
I googled hard, read all kinds of manuals and forums, still no solution. Would appreciate any help.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

batcloud wrote:but has other unintended consequences and breaks that application.
a) What other application?
b) What did you change?
c) Do you get error messages?
batcloud
Forum Newbie
Posts: 15
Joined: Sun Jun 10, 2007 3:04 pm

Post by batcloud »

a) What other application?
WordPress 2.2

b) What did you change?
Put a plugin to make it store Username and User Password in the root / so that its available for other applications.
Activating plugin causes weird effects like inability to logout etc. It used to be ok during the previous version of WP.
Didn't do a complete test for other issues, so not sure what else is the problem.

c) Do you get error messages?
No error messages.

For these reasons, I thought of just accessing the cookies from another directory instead of usin plugins for WP. ALso, I plan to integrate other applications like a Forum, Photo Upload etc, and would rather go with first principles than a plugin for WP.

Thanks Volka!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

preface: I'm unfamiliar with WordPress and just looked at the code.

There's a file wp-settings.php that defines two cookies affecting the cookie availabillity
If you replace

Code: Select all

if ( !defined('COOKIEPATH') )
	define('COOKIEPATH', preg_replace('|https?://[^/]+|i', '', get_option('home') . '/' ) );
if ( !defined('SITECOOKIEPATH') )
	define('SITECOOKIEPATH', preg_replace('|https?://[^/]+|i', '', get_option('siteurl') . '/' ) );
by

Code: Select all

if ( !defined('COOKIEPATH') )
  define('COOKIEPATH', '/' );
if ( !defined('SITECOOKIEPATH') )
  define('SITECOOKIEPATH', '/' );
all cookies set by WordPress should be available within the whole domain.
batcloud
Forum Newbie
Posts: 15
Joined: Sun Jun 10, 2007 3:04 pm

Post by batcloud »

Volka,
Thanks for your suggestions. Nice of you to take the time to read WordPress code.
I made the changes, and also made similar changes to the function:
if(!function_exists('wp_clearcookie'))
so that cookies are cleared on logout.
Then cleared all cookies in the site from my browser, and now seems to work fine.
Post Reply