The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Cookies with multiple URLs
Discuss Cookies with multiple URLs in the PHP Development forum on Dev Shed. Cookies with multiple URLs PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 9th, 2012, 02:24 PM
|
|
|
|
Cookies with multiple URLs
I have a web application with three basic entree points - http://www.mysite.com/index.php?page=123. Displays a page if $_SESSION['logged_on'] if is set, otherwise displays a logon page.
- http://www.mysite.com/ajax.php?task=123&data=abc. Responds to AJAX requests if $_SESSION['logged_on'] is set, otherwise sends 404 header and displays missing page text.
- http://www.mysite.com/remote.php?hash=asdlkfjasdf&data=123. Updates a database, displays some HTML, and/or supplies a document if hash is correct, otherwise sends 404 header and displays missing page text.
My server is set up so http://www.mysite.com and http://mysite.com point to the same directory.
Everything was working great until index.php set a cookie, and then later ajax.php tried to write to the same cookie.
I then started looking at the various URLs using Firebug both with and without the www, and found that some had cookies associated with them, and others did not.
Can anyone explain what is going on? Thanks
|

November 9th, 2012, 02:42 PM
|
 |
Sarcky
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
Cookies are associated with the domain that set them. If you fail to set the 4th argument to setcookie() (the "domain" argument), then the cookie binds to whatever domain you happen to have hit.
If you go to yoursite.com/index.php which fires an ajax request to www.yoursite.com/ajax.php, ajax.php won't see the cookies set by index.php unless all your setcookie calls bind to ".yoursite.com". The dot at the beginning makes it a wildcard, critical to its success.
Read the manual page for setcookie for more.
__________________
HEY! YOU! Read the New User Guide and Forum Rules
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002
Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
|

November 10th, 2012, 08:51 AM
|
|
|
Thanks Dan,
It looks like sessions experience the same behavior, and their associated cookie binds to only one domain.
I did what you suggested, but then my cookies and sessions could get all out of whack. By the way, I am using sessions and cookies for user authentication, I want to ensure that when the user logs off, he/she logs off domains both with and without www.
This problem relates to how cookies are available to the domain that set them and higher domains, and how cookies set by lower domains will be available to that domain.
For instance, if I do Action A at http://www.mysite.com and then do Action B at http://mysite.com, it might create a different state had I done Action A at http://mysite.com and then do Action B at http://www.mysite.com.
This doesn't seem right, and I want to see similar behavior.
Looks like my options are: - Set the domain of the cookies as you described, and the same for sessions using session_set_cookie_params().
- Redirect to a single URI. How would this best be done?
- Some other solution that I didn't think of?
Any advise? Thanks
|

November 10th, 2012, 10:00 AM
|
 |
Lost in code
|
|
|
|
|
You can set up a redirect rule to force them to one domain pretty easily using mod_rewrite.
|

November 10th, 2012, 10:17 AM
|
|
|
|
Thank you E-Oreo,
So don't bother with having the PHP application trying to set cookie domains and cookie session domains to a common domain, and instead just use mod_rewrite, correct?
|

November 10th, 2012, 12:09 PM
|
 |
Sarcky
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
Both #1 and #2 are easy. As oreo said, #2 is an easy redirect rule. #1 is just a find/replace of all your setcookie calls. There can't be many.
You would also have to set the session cookie's domain with session_set_cookie_params
|

November 11th, 2012, 06:23 AM
|
|
|
|
Thanks Dan,
I agree that both are easy fixes. I am just curious whether there is a de facto standard fix to this problem, or whether one has advantages over the other.
For instance, the mod_rewrite isn't as portable to other HTTP servers, however, that isn't a problem for my case.
|

November 11th, 2012, 04:12 PM
|
 |
Sarcky
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
|
There is no de-facto "industry standard" fix for this. Some sites use oreo's solution, some use mine.
|

November 11th, 2012, 08:06 PM
|
|
|
So is Oreo's or your solution the best 
|

November 11th, 2012, 09:38 PM
|
|
Contributing User
|
|
Join Date: Aug 2011
Location: Sydney Australia
|
|
Quote: | Originally Posted by NotionCommotion So is Oreo's or your solution the best  |
You say "to-may-to", I say "to-mar-to".
|

November 12th, 2012, 09:12 AM
|
 |
Sarcky
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
|
There is no "best" in this scenario. Do whatever you think is most convenient for you and your users. Both solutions are 100% effective.
Oreo's solution is slightly more future-proof than mine because it doesn't open you up to problems if you write a setcookie() line in the future and forget to set the host properly.
Mine is slightly more portable than Oreo's because it allows you to move your code to a webserver or host without Oreo's rewrite rule.
|

November 12th, 2012, 09:30 AM
|
|
|
|
Thanks Dan, So, I hear you say they are both considered good approaches and commonly used. I am okay with that. I just wanted to make sure I did not chose a direction which technically works but no one really uses it.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|