• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Codeigniter Sessions Again.

#1
[eluser]Solarpitch[/eluser]
Hey,

I'm having a crazy problem with these CI sessions. Although I love using CI, the sessions can really grind my gears!

I have an authentication script that will log in and user and direct them to a profile page... happy days. But if I click on a link in the page or reload it for what ever reason I get fired back to the login page again. It's almost like CI uses the session once then just discards the fact that it was set at login.

Have a look at this if you get a sec...

LOGIN CONTROLLER
Code:
<?php
class Login extends Controller {

    function Login()
    {
        parent::Controller();
    }

    function index()
    {
    
//~~~
$data['title'] = "";
$this->load->view('template/header', $data);
//~~~
        
// SET VALIDATION RULES
$this->validation->set_rules('user_name', 'username', 'required');
$this->validation->set_rules('user_pass', 'password', 'required');
$this->validation->set_error_delimiters('<em>','</em>');
        
// has the form been submitted and with valid form info (not empty values)
if($this->input->post('login'))
{
if($this->validation->run())
{
$user_name = $this->input->post('user_name');
$user_pass = $this->input->post('user_pass');
                
$row_return = $this->user_model->check_valid_user($user_name, md5($user_pass));
                
if($row_return > 0)
{
                
// user has been logged in

$this->session->set_userdata('logged_in', TRUE); // SET SESSION IF LOGIN TRUE
redirect('profile/index/');

}
else
{
$this->session->set_flashdata('message', 'Incorrect password.');
redirect('login/index/'); // IF FALSE FIRE BACK TO LOGIN
}
}
else
{
$this->session->set_flashdata('message', 'A user does not exist for the username specified.');
redirect('login/index/');
}
            
}

    
$this->load->view('content/login/index');
//~~~
$this->load->view('template/footer', $data);
//~~~
}
    


}

?&gt;


PROFILE CONTROLLER: Constructor

Code:
class Profile extends Controller {

function Profile()
{
parent::Controller();
        
// Check if user is logged in
        
if( ! $this->session->userdata('logged_in')){ // CHECK IF USER IS LOGGED IN (function is a helper)

redirect('login/index/');
}
        

}


HELPER

Code:
// Get CodeIgniter Object
$CI =& get_instance();


// Session variable to check if user is logged in
$user_session_var = 'logged_in';

if( ! $CI->session->userdata($user_session_var))
{
// user is not logged in
return 0;
}
else
{
// User logged in
return 1;
}
}

#2
[eluser]jalalski[/eluser]
Make sure that you have the domain name right. I had a similar situation with a site where I logged in through domain.com, but the next pages were for www.domain.com, which meant the session wasn't recognized.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.