跨域登录检查


Cross domain login check?

我有bookmarklet。如果我打开一个随机的页面(不是我的)并点击书签,我想检查用户是否登录了我的页面。

我已经使用Access-Control-Allow-Origin做跨域AJAX请求,但看起来这里没有会话ID或cookie发送。

有办法做到这一点吗?

Alex是对的!这是完整的溶液。(不支持IE8和IE9!)

您需要在客户端设置withCredentials。从jQuery 1.5.1开始,您可以像下面这样做(源代码)。对于旧版本遵循白兔。

$.ajax({
   url: a_cross_domain_url,
   xhrFields: {
      withCredentials: true
   }
});

在服务器端,你必须允许设置选项,允许凭据和允许起源。不允许使用通配符!但是你可以从请求头中读出源:)

// auto adapted Access Control to origin from request header.
$headers = apache_request_headers();
foreach ($headers as $header => $value) {
    if ($header == 'Origin')
        header('Access-Control-Allow-Origin: ' . $value, true);
}
// send cookies from client
header('Access-Control-Allow-Credentials: true', true);
// allow all methods
header('Access-Control-Allow-Methods: GET, POST, OPTIONS', true);

您必须将凭据标志设置为true以及标题Access-Control-Allow-Credentials

请参阅此处:Firefox:带有凭据的跨域请求返回空