0

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();
Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58

2 Answers2

0

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

Hikmat Sijapati
  • 6,869
  • 1
  • 9
  • 19
  • yes i understand this situation, what i wanted to happen is to set the session of my subdomain using the root domain. but after login function.. and try other function to my subdomain i'm not logged in anymore. – John Christian De Chavez Jan 10 '17 at 06:23
  • It is not a good way to use session of one domain to another?In case of root domain and subdomain check first...whether this domains are sharing same CI system files or not? – Hikmat Sijapati Jan 10 '17 at 06:26
  • why it is not a good way? then what would be the possible solution for my situation? all i want is to set the session of subdomain using root domain via ajax. so that for every call to the functions of subdomain its already logged in and have access to functions. – John Christian De Chavez Jan 10 '17 at 06:34
  • If both domains contains different CI system files..Then not possible to use session of one domain to another. – Hikmat Sijapati Jan 10 '17 at 06:38
  • So you have to run multiple applications sharing same CI system files.. – Hikmat Sijapati Jan 10 '17 at 06:41
  • for more see here...https://philsturgeon.uk/codeigniter/2009/07/08/Create-an-Admin-panel-with-CodeIgniter/ – Hikmat Sijapati Jan 10 '17 at 06:41
  • Hello i already solved my problem.. please see my own answer. if you have something to suggest you may do so. thank you Mr @Hikmat Sijapati – John Christian De Chavez Jan 10 '17 at 10:35
0

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

Community
  • 1
  • 1