Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to set session in Codeigniter?

I'm reading the Codeigniter 2.2 tutorial and I am not clear on how to use sessions for logging in.

Say I have a login.php, which checks user data with the database. Then if its ok then I should set the session in a controller?

  $this->load->library('session');

And then in say admin.php page I should check if session exists by? :

 $this->session->user_data('item'); ??

Or how do I check if the person is logged in?

Thank you

like image 674
Vegan Sv Avatar asked Mar 26 '15 21:03

Vegan Sv


People also ask

How check session is set in CodeIgniter?

3 Answers. Show activity on this post. $this->session->set_userdata('some_name', 'some_value'); But before that ensure that you have the session library included.

How does session work in CodeIgniter?

If sessions data does not exist (or if it has expired) a new session will be created and saved in the cookie. If a session does exist, its information will be updated and the cookie will be updated. With each update, the session_id will be regenerated.

How can store session in database in CodeIgniter?

CREATE TABLE IF NOT EXISTS `ci_sessions` ( session_id varchar(40) DEFAULT '0' NOT NULL, ip_address varchar(16) DEFAULT '0' NOT NULL, user_agent varchar(50) NOT NULL, last_activity int(10) unsigned DEFAULT 0 NOT NULL, user_data text DEFAULT '' NOT NULL, PRIMARY KEY (session_id) );

How set multiple session in CodeIgniter?

With the userdata() method, you can retrieve the fields you saved during the creation of the session. For example, you pass the $array_of_admin_data array , which holds some information like userType . If you want to retrieve this value, you must call userdata() like this: $this->session->userdata('userType');


2 Answers

Based on the docs, to do anything custom within session you need to load the session library. If you plan to use session throughout your application, I would recommend autoloading the library. You do this from within config/autoload.php.

$autoload['libraries'] = array('session');

Then you won't have to use $this->load->library('session'); on every page.

After the library is loaded, set your custom information, maybe based off some information from your database. So in your case, this would be in login.php:

$this->session->set_userdata('userId', 'myId'); where userId would be the name of the session variable, and myId would be the value.

Then, on subsequent pages (admin.php), you could check that the value is there.

if($this->session->userdata('userId') == '') { //take them back to signin }

like image 81
justanotherprogrammer Avatar answered Sep 28 '22 01:09

justanotherprogrammer


To set user session

$the_session = array("key1" => "value1", "key2" => "value2");
$this -> session -> set_userdata($the_session);

To read user session

$foo = $this -> session -> userdata('key1');

You need $this->load->library('session'); every time prior you use CI session functions. Or you can set it up in autoload.php $autoload['libraries'] = array('session');

like image 31
VVLeon Avatar answered Sep 28 '22 01:09

VVLeon