Reputation: 43788
Does any one know there have any way I can share a information (it could be cookie, session, variable...) within 2 difference domains but in the same server?
Example:
I have 2 domain inside the same server, one is called exmaple.com.au and the other one is friendly.com.au. Does there any way that I can share a variable for both domains? (let say $valiable = 'exampleLogin')
Upvotes: 1
Views: 168
Reputation: 401182
As others said, you cannot use a cookie (which means no session either) to share information between different domains, because of the Same Origin Policy implemented in web browsers.
So, you'll end up using either files (as you are on the same server), or a database, to store the informations you want to share.
The problem you will have is to "attach" this information to the user on both sites ; you might need a way to create a cookie on both sites when the user logs in ; a 1x1 transparent gif, or a 1x1 iframe are ways to do that : just pass some token that contains the informations the second site needs to create its cookie.
If what you want is to get a user identified on two (or more) websites without having to log in twice, you might be interested in what is called Single sign-on.
There are a couple of already existing solutions that exist in PHP (I've not used either of those, so I cannot recommend any).
You might also be interested in something like OpenID, which is used here on SO ;-)
Upvotes: 0
Reputation: 19644
This is commonly solved by using a transparent GIF.
For example, on site1.com, include an image:
<img src="http://site2.com/mybug.php?segment1=data1&segment2=data2" height="0" width="0">
Site2 then sets a cookie (in mybug.php) that it will read on subsequent accesses to Site2, based on the name/value pairs in the querystring, and returns a transparent gif in the body of the http response.
This, for example, is how advertising targeting systems do their magic. It can be circumvented by browser security settings, and may require you to include a privacy policy file to work reliable.
The caveat is that you can't just trust this data on your server if it is at all sensitive; in other words, you don't want to use this to transmit usernames, passwords, or the like. If you want to send sensitive data you'll need to use a shared secret and use a reasonably secure encryption mechanism and encrypt at least the values.
Upvotes: 0
Reputation: 12248
You could either $_POST
or $_GET
from one domain to the other, or read and write from the filesystem or a shared database.
Upvotes: 0
Reputation: 9082
That's a pretty broad question.
Cookie/Session - in reality are pretty much the same thing, you can't share them across domains (it's a browser restriction)
Variables - I don't even know what you would mean by "share variables". You can't do this between php scripts at all ("Share-nothing architecture")
A common way would be to store the data in a database (possibly a file if on the same server) from one site, and then you can read it from either site.
Upvotes: 0
Reputation: 522626
Browsers adhere to a "same origin" policy for cookies, so setting a cookie from one site can't affect another. The physical server doesn't matter in this case.
You could use one site for login purposes and exchange data via POST or GET, but you'd still have to maintain two separate cookies. If both sites have access to the same database, it's probably best to use separate login forms for both, but post a notice ala "You can use your login from sitex.com if you're already registered!"
Upvotes: 0
Reputation: 13483
Not with cookies or sessions (as I understand them).
You could store stuff in files or a database if both sites have access.
Alternatively, you may want to look into cURL.
Upvotes: 2