A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearCookies across subdomains and hosts - page 1
User InfoPosts
Cookies across subdomains and hosts#1
In the application I'm writing using a combination of development environments and languages, I have need of accessing a cookie from two different subdomains, each on a separate host.

The cookie is being set on www.mydomain.com using the PHP code that follows, and I'm attempting to access it from distant.mydomain.com on a separate host.

setcookie('token', base64_encode(serialize($token)), time()+10800, '/', '.mydomain.com');


I'm trying to access the cookie from distant.mydomain.com using the following code:

if (isset($_COOKIE['token'])) { /* do stuff */ }


The problem: distant.mydomain.com is not finding the cookie. The if statement just mentioned returns false, even though the cookie exists.

I have verified that the cookie that is set is for mydomain.com (by checking my Firefox cookies). I can't think of any reason this wouldn't be working.

Using the same setcookie code, I have an old application running exclusively on the www.mydomain.com host, and that application is able to access the cookie across domains. This makes me suspect that the problem has to do with separate hosts.

Just in case any of the following information is pertinent:
- www.mydomain.com is IIS 6.0
- distant.mydomain.com is Apache 2.2.9
- Both servers use PHP 5.2.x
- Both servers are operating on Windows Server 2003

If there is any further information I can provide in order to better describe the problem, please let me know!

posted date: 2008-12-17 15:11:00


Re: Cookies across subdomains and hosts#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-17 15:11:01


Re: Cookies across subdomains and hosts#3
..and what's your question?

posted date: 2008-12-17 15:13:00


Re: Cookies across subdomains and hosts#4
Sorry about that; I still haven't figured out why the second half of my post didn't make it through when I hit submit.

posted date: 2008-12-17 15:38:00


Re: Cookies across subdomains and hosts#5
there's no question though still :)

posted date: 2008-12-17 15:43:00


Re: Cookies across subdomains and hosts#6
Curses! You know how the second time you have to write a post/e-mail/whatever, it's never as good as the first time? goes off to edit

posted date: 2008-12-17 15:44:00


Re: Cookies across subdomains and hosts#7
Does one of the subdomains use an underscore ? IE has problems accepting cookies from subdomain's that dont follow the URI RFC.This is asumming 'distant' is a placeholder and not the actual subdomain name and of course that you use IE. Although more browsers could very well be effected by as, Fireworks doesn't though.

posted date: 2008-12-17 15:56:00


Re: Cookies across subdomains and hosts#8
From php.net about the 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.The domain that the cookie is available. To make the cookie available on all subdomains of example.com then you'd set it to '.example.com'. The . is not required but makes it compatible with more browsers. Setting it to www.example.com will make the cookie only available in the www subdomain. Refer to tail matching in the ยป spec for details.Basically: Your 4. and 5. parameter needs to be checked: Well, your path seems to be fine, but the domain needs to be changed:Today you block the cookie to all others than domain A, but you want it to be awailable to both domain A and B. This is a bit tricky, but can be solved. Get inspiration on 15seconds ;-)

posted date: 2008-12-17 15:59:00


Re: Cookies across subdomains and hosts#9
The actual domain has a hyphen but all other characters are alpha. The subdomains in question are exclusively alpha.

posted date: 2008-12-17 16:00:00


Re: Cookies across subdomains and hosts#10
hyphens should be fine :)

posted date: 2008-12-17 16:02:00


Re: Cookies across subdomains and hosts#11
I may be interpreting your response incorrectly, but based on the php.net/setcookie documentation, my path and domain should be correct as given in my original question. If they should be corrected, please provide an example of what the setcookie() function call should look like.

posted date: 2008-12-17 16:32:00


Re: Cookies across subdomains and hosts#12
I'd try installing Charles Proxy and see what headers are a) being sent to Firefox to begin with (to set the cookie) and b) which headers are being sent from Firefox to the second server. At least that way you can narrow down where the problem is (browser or server).

posted date: 2008-12-17 16:38:00


select page: « 1 2 »
Copyright ©2008-2017 www.momige.com, all rights reserved.