how to get cookies from a different domain with php and javascript

前端 未结 2 1508
再見小時候
再見小時候 2020-12-16 00:58

Suppose i have a cookie set in first.com say user. Now i want to read that cookie in second.com through javascript and ajax. But it is not working.I have got xmlHttp.status=

相关标签:
2条回答
  • 2020-12-16 01:10

    You can't read cookies from another domain - end of.

    The only way I can think of is to add some code to the 2nd domain that gets the cookies for you and then to place this in a page on the 1st domain, in an iframe.

    You obviously need full access to both domains to be able to do this kind of thing.

    0 讨论(0)
  • 2020-12-16 01:27

    Your problem is that browsers wont let javascript to access different domain. Add:

    header('Content-type: text/html');    
    header('Access-Control-Allow-Origin: *');   
    

    lines to the beginning of cookie.php and it'll work. Still, you wont get the cookie (or at least in Chrome). I couldnt yet figure out why. It seems as if chrome creates a new session for the javascript and wont let that session access previous cookies. Like HttpOnly.

    0 讨论(0)
提交回复
热议问题