We have 2 te开发者_JAVA百科st environments, which are:
- http://test.example.com/ (This is our front end CMS testing site)
- http://test-example/ (This is our internal development site where form data is collected, etc)
Below is the cookie that is written on "test.example.com" domain. The user will reach the page where the cookie is written to their computer and then they will click a link that will take them to "test-example", which is our internal testing domain. I observed when they leave "test.example.com" and are taken to our internal testing domain "test-example" the cookie does not follow therefor I'm not able to read the cookie.
Would anyone be able to help with my code to allow the cookie to be read across our internal testing domain?
<script type="text/javascript">
var cookieName = 'HelloWorld';
var cookieValue = 'HelloWorld';
var myDate = new Date();
myDate.setMonth(myDate.getMonth() + 12);
document.cookie = cookieName +"=" + cookieValue + ";expires=" + myDate
+ ";domain=.example.com;path=/";
</script>
Cookies cannot be shared across domains. This is a tenant of internet security.
Here's what you can do:
- Use a subdomain, e.g. put your internal dev site at http://dev.test-example.com
- Read how Facebook sets cookies cross-domain
This is impossible. The sites don't share a TLD and it would be incredibly inefficient (and somewhat risky from a security point of view) for a site to be able to say "Send my cookies to every site you visit".
Move the internal testing site to internal.test.example.com and see setting cross-subdomain cookie with javascript.
精彩评论