简体   繁体   中英

Access Session data in codeigniter

 $username = $this->input->post('username');
$password = $this->input->post('password');
$result = $this->AccountModel->login($username,$password);
if($result){
foreach($result as $row){
$loggedin = array('Admin_name' => $row['username'], 'is_loggedin' => TRUE);
$this->session->set_userdata($loggedin);

How would i be able to access ['Admin_name'] and ['is_loggedin'] without having to run this code and running a foreach loop $this->session->all_userdata()

try this

 $this->session->userdata('Admin_name');
 $this->session->userdata('is_loggedin');

From "Retrieving Session Data" section of Session Class docs:

Any piece of information from the session array is available using the following function:

$this->session->userdata('item');

So you want:

$this->session->userdata('Admin_name');

The docs are your friend ;)

You can access as below:

$Admin_name = $this->session->userdata('Admin_name');

For session library, please check the CI User guide on sessions

u can access it by using this line

$this->session->userdata['Admin_name'];

it should work ..if it dosen't ..just print the session variables using any of the line below

echo '<pre>';print_r($this->session->all_userdata());exit;
echo '<pre>';var_dump($this->session->all_userdata());exit;

once u see the structure of userdata ..u can easily access any session variables in there just like any other array ..just use

$this->session->userdata['array_key'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM