CodeIgniter Forums
How to check if the user is logged in. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: How to check if the user is logged in. (/showthread.php?tid=51252)



How to check if the user is logged in. - El Forum - 04-26-2012

[eluser]Cinghius[/eluser]
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



How to check if the user is logged in. - El Forum - 04-26-2012

[eluser]WanWizard[/eluser]
That greatly depends on the mechanism used for authentication and authorisation, so there isn't one single answer.


How to check if the user is logged in. - El Forum - 04-26-2012

[eluser]Cinghius[/eluser]
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 ()


How to check if the user is logged in. - El Forum - 04-26-2012

[eluser]WanWizard[/eluser]
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?


How to check if the user is logged in. - El Forum - 04-26-2012

[eluser]Cinghius[/eluser]
Code:
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


How to check if the user is logged in. - El Forum - 04-26-2012

[eluser]WanWizard[/eluser]
This doesn't really explain much, other than it's pretty useless to load a view and then redirect...


How to check if the user is logged in. - El Forum - 04-26-2012

[eluser]cPage[/eluser]
Code:
<?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 */