EllisLab text mark
Advanced Search
     
How to check if the user is logged in.
Posted: 26 April 2012 09:24 AM
Joined: 2012-04-26
7 posts

I am writing to know how to check if the user has logged in or not, before doing the redirect on the home page, and display other content than the home page as not logged in.
thank you so much

 
Posted: 26 April 2012 10:29 AM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4489 posts

That greatly depends on the mechanism used for authentication and authorisation, so there isn’t one single answer.

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM <= LOOKING FOR A NEW MAINTAINER!

 
Posted: 26 April 2012 10:37 AM   [ # 2 ]   [ Rating: 0 ]
Joined: 2012-04-26
7 posts

I have a controller through a login function and various methods to check whether the user can log in, or not. In the end, does a redirect on the home page. I would know, if the user who is logged on the homepage, sees things other than not logged.
To do this, I would have to work on the method $ this-> tank_auth-> is_logged_in ()

 
Posted: 26 April 2012 10:43 AM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4489 posts

I’m not familiar with tank_auth, but I assume is has a method to check if a user is logged on or not?

A quick check of the library shows that is indeed is_logged_in(), so if you know that, what is your question?

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM <= LOOKING FOR A NEW MAINTAINER!

 
Posted: 26 April 2012 10:59 AM   [ # 4 ]   [ Rating: 0 ]
Joined: 2012-04-26
7 posts
if ($this->tank_auth->is_logged_in()) {         // logged in
$this->load->view("/shared/overlay",array(
'url' => site_url(),
'title' => 'LOGIN OK',
'text' => " Login ok!"
     
));
redirect(''); 

What I write on the home page after the redirect to check if the user is logged in, how do I make the control of that method?
I apologize if I have not explained well

 
Posted: 26 April 2012 11:18 AM   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4489 posts

This doesn’t really explain much, other than it’s pretty useless to load a view and then redirect…

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM <= LOOKING FOR A NEW MAINTAINER!

 
Posted: 26 April 2012 04:44 PM   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2012-04-06
54 posts
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Login extends CI_Controller {

    
function __construct()
    
{
 parent
::__construct();  
 
$this->load->library('session');
 
$this->load->helper('url');
 
$this->load->library('encrypt');  
    
}

    
function index()
    
{
 
//Check to see if some key session userdata is still available
 
if($this->session->userdata('logged_in') && $this->session->userdata('username'))
 
{
     
// ===========================
     // = User is still logged in =
     // ===========================
     
     //Dont bother with the login page, just send em on to the start page
     
redirect('intro');
     
   
}
   
else
  
{
            
// =========================
            // = User is not logged in =
            // =========================
            
            // Lets use the form validation library to keep things simple & robust
            
$this->load->library('form_validation');
            
$this->form_validation->set_rules('username''Username''required|callback__verify_username_password');
            
$this->form_validation->set_rules('password''Password''required');
            
            
//We have a custom method that is checking the username & password validity
            
$this->form_validation->set_message('_verify_username_password'"Username or password is incorrect");

            if(!
$this->form_validation->run())
            
{
                
//Si le login a √©chou√© charger la vue login
                
$this->load->view('login');
            
            
}
   
else
   
{
    
//Set our custom session userdata to be checked by our secure controller
                
$this->session->set_userdata('logged_in' ,TRUE);
                
$this->session->set_userdata('username' $this->input->post('username'));
 
                
// =================================
                // = Redirect based on user intent =
                // =================================
                
                //Check if user was trying to access a specific URL before login procedure
                
if($this->session->userdata('REDIRECT'))
                
{
                    
//Save the REDIRECT
                    
$redirect $this->session->userdata('REDIRECT');
                    
                    
//Unset the REDIRECT otherwise we end up in a loop
                    
$this->session->unset_userdata('REDIRECT');
                    
                    
//Away we go..!
                    
redirect($redirect);
                    
                
}
  
else
  
{
                    
// There is no REDIRECT set, just send em to the start page
                    
redirect('intro');
                
}
            }
        }
    }
    
    
/**
     * Verifies that the username and password are correct
     *
     * @return boolean
     **/
    
function _verify_username_password($str)
    
{
  $this
->load->model('Membre_m');
  
$accept FALSE;
  
$username $str;
        
$password trim($this->input->post('password'));
  
$accept $this->Membre_m->verif_membre_password($username,$password);
  return 
$accept;
    
}
}

/* End of file login.php */
/* Location: ./application/controllers/login.php */