Welcome Guest, Not a member yet? Register   Sign In
How to check if the user is logged in.
#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 */


Messages In This Thread
How to check if the user is logged in. - by El Forum - 04-26-2012, 06:24 AM
How to check if the user is logged in. - by El Forum - 04-26-2012, 07:29 AM
How to check if the user is logged in. - by El Forum - 04-26-2012, 07:37 AM
How to check if the user is logged in. - by El Forum - 04-26-2012, 07:43 AM
How to check if the user is logged in. - by El Forum - 04-26-2012, 07:59 AM
How to check if the user is logged in. - by El Forum - 04-26-2012, 08:18 AM
How to check if the user is logged in. - by El Forum - 04-26-2012, 01:44 PM



Theme © iAndrew 2016 - Forum software by © MyBB