doudou_3636
2017-01-10 06:12
浏览 52
已采纳

使用ajax设置子域的会话

I'm using a domain like www.example.com and a sub-domain like sub.example.com.

I'm trying to call the Login function of sub.example.com. In the end I cant set the session of sub.example.com for future functions to be called.

I'm using CodeIgniter in my sub-domain and my root domain just has an HTML page with native JavaScript. I wanted to use sub.example.com as my endpoint for all my sub-websites.

I tried changing the config of my subdomain(CI) to

$config['cookie_prefix']    = "";
$config['cookie_domain']    = ".example.com";
$config['cookie_path']      = "/";
$config['cookie_secure']    = FALSE;

and also the index.php file of my www.example.com to:

session_set_cookie_params(0, '/', '.example.com');
session_start();

图片转代码服务由CSDN问答提供 功能建议

我正在使用像 www.example.com 这样的域和一个子域 比如 sub.example.com

我正在尝试调用sub.example.com的 Login 函数。 最后我无法设置sub.example.com的会话以便将来调用函数。

我在子域中使用CodeIgniter而我的根域只有一个HTML页面 使用原生JavaScript。 我想使用 sub.example.com 作为我所有子网站的终端。

我尝试将子域(CI)的配置更改为< / p>

  $ config ['cookie_prefix'] =“”; 
 $ config ['cookie_domain'] =“。example.com”; 
 $ config ['cookie_path']  =“/”; 
 $ config ['cookie_secure'] = FALSE; 
   
 
 

以及我的www.example.com的index.php文件:

  session_set_cookie_params(0,'/','。example.com'); 
session_start(); 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongyi5425 2017-01-10 10:34
    已采纳

    I solved my problem by setting header("Access-Control-Allow-Credentials: true");[SERVER-SIDE] and $http.defaults.withCredentials = true;[CLIENT-SIDE] using Angular $httpProvider upon request

    And also don't forget to set the header("Access-Control-Allow-Origin: www.example.com");[SERVER-SIDE] my root domain to have an access to my sub-domain.

    but if you have multiple domains/sub-domains to allow you can use below code

    $http_origin = $_SERVER['HTTP_ORIGIN'];
    
    if ($http_origin == "http://www.domain1.com" || $http_origin == "http://www.domain2.com" || $http_origin == "http://www.domain3.info")
    {  
        header("Access-Control-Allow-Origin: $http_origin");
    }
    

    for more info Access-control-allow-credentials

    点赞 打赏 评论
  • dsla94915 2017-01-10 06:21

    In codeigniter you not need to session_start();.Just load session library and then try .

    Load session library as:

    $this->load->library('session');//from models,controllers and view..
    

    But if you are outside from models,views and controller..Load CI session library as follows:

    1.First create instance of CI

    $this->CI =& get_instance();
    

    2.Then Load Library

    $this->CI->load->library('session');
    

    For more see here https://codeigniter.com/userguide3/libraries/sessions.html#destroying-a-session

    点赞 打赏 评论

相关推荐 更多相似问题