views:

550

answers:

5

Does anyone know whether I can set a session value for the current domain, and use this session for another domain?

For example:

when I set session in domain www.aabc.com and I wish this session to work in domain www.ccc.com as well -- I click a button in www.aabc.com and change the header to www.ccc.com?

A: 

No. Sorry. :)

dirtside
+2  A: 

You can only set cookies for your domain (and other sites on your domain, like subdomains, if I remember correctly).

This is (mainly ?) for security reasons : else, anyone could set cookies for any website... I let you imagine the mess ^^

(The only way to set cookies for another domain seem to be by exploiting a browser's security hole - see http://en.wikipedia.org/wiki/Cross-site_cooking for instance ; so, in normal cases, not possible -- happily)

Pascal MARTIN
sorry make.. my questions was setting session for difference domain...
Jin Yong
Well, you edited your question to put "session" instead of "cookie" just while I was writting my answer ; my answer remains the same, as the link between an user and his session is generally kept within a cookie.And, as the session's data is generally kept in a file on the webserver, it's even harder to share it between websites(though, I suppose, if your server is not properly secured, you could access session files from another website hosted on it -- but not good ^^ )
Pascal MARTIN
A: 

You cannot access both domains sessions directly, however, there are legitimate solutions to passing session data between two sites that you control. For data that can be tampered with you can simply have a page on domain abc.com load a 1px by 1px "image" on xyz.com and pass the appropriate data in the querystring. This is very insecure so make sure the user can't break anything by tampering with it.

Another option is to use a common store of some kind. If they have access to the same database this can be a table that domain abc.com stores a record in and then passes the id of the record to domain xyz.com. This is a more appropriate approach if you're trying to pass login information. Just make sure you obfuscate the ids so a user can't guess another record id.

Another approach to the common store method if these two domains are on different servers or cannot access the same database is to implement some sort of cache store service that will store information for a time and is accessible by both domains. Domain abc.com passes in some data and the service passes back an ID that domain abc.com sends to domain xyz.com which then turns back to the service asking for the data. Again, if you develop this service yourself make sure you obfuscate the ids.

Spencer Ruport
A: 

I had to set this up at my last job. The way it was handled was through some hand-waving and semi-secure hash passing.

Basically, each site, site A and site B, has an identical gateway setup on each domain. The gateway accepts a user ID, a timestamp, a redirect URL, and a hash. The hash is comprised of a shared key, the timestamp, the user ID.

Site A generates the hash and sends all of the information listed above to the gateway at site B. Site B then hashes the received passed user ID and timestamp with the shared key.

If the generated hash matches the received hash, then the gateway logs the user in and loads their session from a shared memory table or memcached pool and redirects the user to the received redirect url.

Lastly, the timestamp is used to be able to determine an expiration time for the provided passed hash (e.g.: the hash was only valid for x time). Something around 2.5 minutes is what we used for our TTL (to account for network lag and perhaps a refresh or two).

The key points here are:

  • Having a shared resource where sessions can be serialized
  • Using a shared key to create and confirm hashes (if you're going to use md5, do multiple passes)
  • Only allow the hash to be valid for a small, but reasonable amount of time.
  • This requires control of both domains.

Hope that was helpful.

Justin Johnson
A: 

Can someone post an example code snippet?