【问题标题】:I want to get session data from subdomain我想从子域获取会话数据
【发布时间】:2014-04-30 19:50:23
【问题描述】:

我在 m.example.com 上,想从 www.example.com 获取会话

php代码(session.php):

   <?php

  ini_set('session.cookie_domain', '.jeelplus.com'); 
session_set_cookie_params(0, '/', '.jeelplus.com'); 

header("Access-Control-Allow-Origin: *"); 
header('Access-Control-Allow-Methods: POST, GET');
header('Access-Control-Allow-Headers: Authorization, X-Requested-With, Content-Type, Origin, Accept');
//header('Access-Control-Allow-Credentials: true');

   session_start();  
  print_r($_SESSION);
  echo('11111111111111111');
  exit;
?>

jquery 代码:

function userIsLoggedIn(){
       var logged_in = null;    

       $.ajaxSetup({cache: false, crossDomain:true, headers: {"X-Requested-With": "XMLHttpRequest"}, xhrFields: { withCredentials: true }})
       $.get("http://www.example.com/session.php", {requested: 'foo'}, function (data) {
            alert(data);
           logged_in = data;    
        });  
}

回复:

Array
(
)
11111111111111111

缺少哪些步骤??

【问题讨论】:

  • 您没有通过 AJAX 请求发送会话 cookie。
  • 我该怎么做? @Tularis
  • 你能给我提供完整的代码吗?
  • Cookie 在 HTTP 标头中发送,应该可以跨子域共享。检查是否正在发送 cookie 的最佳方法是使用检查器 (chrome) 或 firebug (firefox) 并查看 AJAX 发送的请求。您应该能够在请求标头中看到 cookie。
  • 我需要什么,使用 ajax 从服务器读取会话并启用跨域但会话返回空。

标签: php jquery cross-domain


【解决方案1】:

不为子域管理会话。你必须使用cookie。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 2012-06-25
    • 1970-01-01
    • 2012-12-25
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多