Forum Moderators: coopster

Message Too Old, No Replies

Set php.ini for cookie subdomains for multiple sites

Setup php.ini for multiple domain cookie setting

         

onematchfire

1:01 am on Sep 9, 2005 (gmt 0)

10+ Year Member



I need to set a couple servers to accept the cookies of the other server. They are all for the same domain, they each have a different subdomain. The trick is that on each server is a second site so I can't explicitly post a single domain, as it has to work for both domains.

eg.

server1 hosts www.mysite.com and www.yoursite.com
server2 hosts ww2.mysite.com and ww2.yoursite.com

I want both machines to accept cookies from both:
.mysite.com and .yoursite.com

So how do I set the php.ini

; The domain for which the cookie is valid.
session.cookie_domain = .mysite.com, .yoursite.com

?

or

session.cookie_domain = .mysite.com .yoursite.com

or is there a keyword that can be used to use the domain whichever site is being served

such as

session.cookie_domain = .$HTTP_REFFER['domain'].com

?

StupidScript

2:55 am on Sep 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Set two cookies:

setcookie(Name,Value,Expire,Path,Domain)

One for each domain.

From the manual [php.net]:

Re: 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.

Re: Domain:
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.

coopster

2:24 pm on Sep 9, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, onematchfire.

I notice one other thing here and want to pose a question. You are referring to cookies (and SS has offered up the solution), but in your message you refer to sessions. At least the php.ini directive you are referring to is dealing with session cookies.

Sharing a session across servers is a bit more advanced. If the same subdomains reside on the same server, not as difficult. But if they are on different boxes, your strategy for sharing session information is going to be a bit more challenging.

onematchfire

5:51 pm on Sep 9, 2005 (gmt 0)

10+ Year Member



Thanks SS and Coopster,

Our need is to have two (and then more) load balanced webservers. Each webserver will serve pages for two different domains (that share a single code set). We have already databased the session data, but will need each webserver to have read/write access to a single PHPSESSID cookie for one or both of our domains. And since each webserver in the cluster will have a different subdomain, each server will need to be able to read/write that single cookie for each domain regardless of the subdomain that particular server has.

I'm pretty sure now that doing this in the php.ini session.cookie_domain is not possible.

So then my expectation is that we'll simply need to add check session cookie function as part of our custom session handler to look for an existing session cookie, and if one does not exist set it with the newly assigned session ID.

Does the latter solution sound right or do you know of any more built in session-related functionality to support this.

Thanks for the help! After doing a bunch of google searches and reading ALL the php.net docs I thought of WebmasterWorld as the best place to go =)

StupidScript

8:57 pm on Sep 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Checking for a session cookie is trivial, so that shouldn't be a problem ... but ...

I'm wondering about the

session_set_cookie_params()
function (more here [us2.php.net]. Maybe you could use that ...?

I haven't used it before. From the docs, I can picture a cascade of individual function calls mangling the heck out of php.ini, but that may not be the case.

Just musing ... searching the PHP functions for session.cookie_domain turns up some possibilities.