跨域登录检查?

时间:2011-12-08 17:19:05

标签: php javascript cross-domain

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

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

有办法做到这一点吗?

2 个答案:

答案 0 :(得分:4)

亚历克斯是对的!这是完整的解决方案。 (它不适用于IE8和IE9!)

您需要在客户端设置withCredentials。从jQuery 1.5.1开始,你可以像下面显示的那样(Source)。对于旧版本,请按white rabbit

$.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);

答案 1 :(得分:2)

您必须将credentials flag设置为true,并将标头设为Access-Control-Allow-Credentials

另见:Firefox: Cross-domain requests with credentials return empty

相关问题