Welcome Guest, Not a member yet? Register   Sign In
[Solved] Unsetting Session In Array
#1

(This post was last modified: 04-26-2017, 11:44 PM by wolfgang1983.)

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.
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

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
Reply
#3

(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
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#4

(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.
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB