• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to check if the user is logged in.

#1
[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

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

#3
[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 ()

#4
[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?

#5
[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

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

#7
[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 */


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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