1

Anybody has any idea how to get $_COOKIE['PHPSESSID'] value in codigniter, which will be remain same unless browser close? Thanks

Toton
  • 139
  • 11
  • 1
    No i don't want this. What i want to get is the current browser session which will be remain unchanged unless closing the browser. – Toton Sep 20 '18 at 13:43

1 Answers1

2

In order to get the session id you first need to verify on application/config/config.php the value for the variable sess_cookie_name. You will find something like:

$config['sess_cookie_name'] = 'ci_session';

As mentioned on this issue How to retrieve cookie value in CodeIgniter?, you get the cookie using the code:

$cookies = $this->input->cookie();

In the cookies variable you have all the cookies from your application. In order to get the session id from the above you do:

$session_id = $cookies->ci_session;

If you just need the session id you can get it directly using the get_cookie function from the cookie_helper:

$this->load->helper('cookie');
$session_id = get_cookie('ci_session');
  • thanks Francisco for your reply, but in your approach will the value of ci_session be same unless i close the browser window? – Toton Sep 21 '18 at 10:09
  • The session could remain the same even if you close the browser because the browser can save this information in cache. Codeignier only creates a new session when there is not a valid one to be used. For instance, the session timeout or the user clear the browse cache. it those cases Codeigniter will generate a new session. Toton, I advise you to read the Session documentation: https://codeigniter.com/user_guide/libraries/sessions.html?highlight=session Codeginiter documentation is really good and give you all the information you need. – Francisco de Castro Sep 23 '18 at 19:36