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

使用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();
  • 写回答

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大