Reputation: 53
I'm using a codeigniter and i have 3 controller, i use the session to store username from the 1st controller and i use this to determine from the other controller if someone is logged in.
Here is a the constructor of the first controller, in this class, i also use the statement where i give a value to $_SESSION['username']
so the other controller use it:
class Login extends CI_Controller {
public function __construct()
{
session_start();
parent::__construct();
}
public function index()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[4]');
if ( $this->form_validation->run() !== false ) {
// then validation passed. Get from db
$this->load->model('Login_Model');
$res = $this->Login_Model->verify_user(
$this->input->post('userName'),
$this->input->post('password')
);
if ( $res !== false ) {
$tempuser = $this->input->post('username');
$_SESSION['username'] = $tempuser;
redirect(base_url());
}
}
$this->load->view('login_view');
}
This is the 2nd controller, on he constructor, i created a conditional statement to determine if someone is logged in, i tested this and it's still ok:
class Pagination extends CI_Controller {
public function __construct() {
parent:: __construct();
$this->load->helper("url");
session_start();
if(!isset($_SESSION['username'])){
redirect(base_url().'index.php/login');
}
}
In the 3rd controller, this is where i encounter the problem, i cannot access the variable where i stored the username, here is the code:
class Blood_Controller extends CI_Controller {
public function __construct()
{
session_start();
parent::__construct();
if(!isset($_SESSION['username'])){
redirect(base_url().'index.php/login');
}
}
It always go inside the if statement even if i enter a rightusername on the 1st controller. can anyone help?
Upvotes: 0
Views: 1023
Reputation: 3170
You can initialize the Session class manually in your controller constructor, use the $this->load->library function:
$this->load->library('session');
Don't need to use session_start() in codeigniter.
Once loaded, the Sessions library object will be available using: $this->session
On the other hand if you don't want to initialize the Session class manually in your every controller constructor, then you can autoload it from your application/config/autoload.php file,just add session class name in $autoload['libraries'] array like this:
$autoload['libraries'] = array('database', 'form_validation', 'session');
Now you can set your session data like this way:
$this->session->set_userdata('user_name', $user_name);
And you can retrieve your session data like this way:
$user_name = $this->session->userdata('user_name');
Go here to learn more about CI session : http://ellislab.com/codeigniter/user-guide/libraries/sessions.html
Upvotes: 0
Reputation: 111
load this "$this->load->library('session'); " in the constructor ie in here
public function __construct()
{
session_start();
parent::__construct();
}
and remove the session_start() and use
`$this->session->set_userdata('username', $tempuser);`
Upvotes: 1
Reputation: 3765
remove all the session_start(); and set it in the config -> autoload.php
change '!==
'to "!=
' and check the session variable value
Upvotes: 0
Reputation: 538
Need to set session data using $_SESSION array in this case instead of CI session library ie. replace $this->session->set_userdata('username', $tempuser);
with $_SESSION['username'] = $tempuser
Upvotes: 0
Reputation: 123
It's always good idea to check if session is already exists.
Try:
if (!session_id())
session_start();
And try to var_dump($_SESSION); in 3rd controller after session_start() to check what is inside.
Upvotes: 0