consider having several domains, all parked on same vhost account, running the same php code and mysql db. the primary domain, primary-example.com does have set a cookie 'test' with value = 'value' I need, to read cookie when someone visits any secondary / parked domain, before I show the page on parked-example.com, so as this would be impossible as cookies are browser-side and we need the visitor to visit primary-example.com in order to have available the cookie. Using DB would not be feasible for this case, so I have made this: on main controler, I check at some point before page delivery: index.php ... // check if this is primary domain or not if( ($_SERVER['SERVER_NAME'] != 'primary-example.com') && ($_SESSION['foreign_cookie'] != 'started') && ($_POST['cookie_remote'] == '') ) { $_SESSION['foreign_cookie'] = 'started'; header('P3P: CP="HONK"'); include 'path/to/template/foreign_cookies.php'; exit; } PHP: ... the file foreign_cookies.php contains: ... <script type="text/javascript" src="http://<?php echo $_SERVER['SERVER_NAME']; ?>/lib/js/jquery-2.1.1.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $.ajax({ type: 'POST', url: 'http://primary-example.com/set_remote_cookie.php', data: 'run=yes'}) .done(function(response) { document.getElementById('cookie_remote').value=response; setTimeout('window.document.forms[0].submit()',1000); }) .fail(function() { window.document.forms[0].submit(); }); }); </script> Please Wait ... <form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="POST"> <input type="hidden" id="cookie_remote" name="cookie_remote" value=""> </form> Code (JavaScript): ... set_remote_cookie.php file contains: ... $cookie_value = $_COOKIE['test']; if( ($_POST['run'] == 'yes') || ($_GET['run'] == 'yes') ) { $reply = "VALUE:$cookie_value"; header('P3P: CP="HONK"'); echo $reply; } PHP: -------------- normally, this setup should work. you can check and see that if you visit parked-example.com page would stop load on first run (session is unset) and show the waiting html page, which then does AJAX post on the primary domain on a file that is called client side (via ajax), which normally can access, read and echo the cookie value. if you try to visit the cookie retriever file: http://primary-example.com/set_remote_cookie.php?run=yes, this would return: "VALUE:value" there seems that ajax runs ok, but the response it retrieves, is missing the value of the cookie ( result I get: "VALUE:" ) please let me know how to resolve this issue! thanks for your time reading this.
I don't have a solution myself.. But I'd be very curious about the actual solution if anyone can come up with one and strongly empathise with your problem, because your problem parallels a problem I've had myself with a CMS project I'd been working on some time back (kind of on-going, I guess, but no set deadline and haven't touched it in over a year, so.. debatable, but.. not important) - Multiple domains for different "communities" and sub-domains for regional and language selection, whilst relying on a cookie for pulling session IDs - Session ID wouldn't carry across to different domains or sub-domains, which meant a user would be logged out any time they switched region or language. Not an operation breaking or site killing problem.. But tediously annoying at times. Subscribing to your thread, and, hopes to both of us that someone may come up with a good solution. Edit: Spelling error.
I am afraid that in my case, it is much more crucial, as this is a part needed to happen on advance of serving the final page. I hope someone expert with cross-domain cookie/session issues will post some solution soon !
Sorry I am having a hard time understanding your question. Can you simply (without code) explain what is needed. Something like: I have domain x and y. I want to store a cookie on y sent by x. Either way, my suggestion would be for you to use jsonp instead of the post.
I need to call an ajax request to a foreign domain, which will be a php file that runs / posts on foreign domain and would echo cookie values... Posting the form simply with an html form, returns echo message with cookie value, posting the form via ajax, cookie is not echoed back / unset. The file in both cases is posted client side (html form or ajax), so normally I expected the php file to echo back the cookie set on foreign domain, but for some reason, it does not work. Anyone know why ?
That is a limitation of ajax. If you post an ajax request to a different domain, you can't access the cookies. You have 2 options: 1) Enable CORS on the secondary domain: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS 2) Use jsonp to make the ajax request: http://json-p.org/ Personally I prefer using jsonp, much easier to implement with similar logic to the standard jQuery ajax.