使用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();

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

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