CodeIgniter Forums
[Solved] Unsetting Session In Array - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: [Solved] Unsetting Session In Array (/showthread.php?tid=67931)



[Solved] Unsetting Session In Array - wolfgang1983 - 04-25-2017

In my admin session i can unset the session like below

PHP Code:
<?php

class Logout extends CI_Controller {

public function 
__construct() {
parent::__construct();
}

public function 
index() {
$admin_session_data $this->session->userdata('admin');

unset(
$admin_session_data['is_logged']);

$this->session->unset_userdata('admin'$admin_session_data['is_logged']);

redirect(base_url('admin/common/login'));
}



In my controller I set the admin session like


PHP Code:
<?php

class Login extends CI_Controller {

public function 
__construct() {
parent::__construct();
}

public function 
index() {
$this->session->set_userdata('admin', array('is_logged' => TRUE));

echo 
anchor('admin/common/logout''Logout');
}



On the logout controller what is the better way of unsetting the session in array I will be adding more in set session later.


RE: Unsetting Session In Array - PaulD - 04-26-2017

Personally I always access the session variable directly.

PHP Code:
// set session item
$_SESSION['is_admin'] = TRUE;

// unset session item
unset($_SESSION['is_admin']); 

Or for an array:

PHP Code:
// set session item array
$_SESSION['is_admin'] = array('is_logged'=>TRUE'is_admin'=>TRUE);

// unset session item array element
unset($_SESSION['is_admin']['is_logged']); 

I think the set_userdata methods are deprecated now.

Also, the unset userdata, I think, can only unset an entire key, not an element inside an array stored in the key.

Whatever it is, accessing the session variable is IMHO cleaner, easier, more concise and more direct.

Best wishes,

Paul


RE: Unsetting Session In Array - wolfgang1983 - 04-26-2017

(04-26-2017, 10:49 AM)PaulD Wrote: Personally I always access the session variable directly.

PHP Code:
// set session item
$_SESSION['is_admin'] = TRUE;

// unset session item
unset($_SESSION['is_admin']); 

Or for an array:

PHP Code:
// set session item array
$_SESSION['is_admin'] = array('is_logged'=>TRUE'is_admin'=>TRUE);

// unset session item array element
unset($_SESSION['is_admin']['is_logged']); 

I think the set_userdata methods are deprecated now.

Also, the unset userdata, I think, can only unset an entire key, not an element inside an array stored in the key.

Whatever it is, accessing the session variable is IMHO cleaner, easier, more concise and more direct.

Best wishes,

Paul

Thanks will try it out


RE: Unsetting Session In Array - wolfgang1983 - 04-26-2017

(04-26-2017, 10:49 AM)PaulD Wrote: Personally I always access the session variable directly.

PHP Code:
// set session item
$_SESSION['is_admin'] = TRUE;

// unset session item
unset($_SESSION['is_admin']); 

Or for an array:

PHP Code:
// set session item array
$_SESSION['is_admin'] = array('is_logged'=>TRUE'is_admin'=>TRUE);

// unset session item array element
unset($_SESSION['is_admin']['is_logged']); 

I think the set_userdata methods are deprecated now.

Also, the unset userdata, I think, can only unset an entire key, not an element inside an array stored in the key.

Whatever it is, accessing the session variable is IMHO cleaner, easier, more concise and more direct.

Best wishes,

Paul

Works fine now thank you Paul.