简体   繁体   中英

If I already logged in then redirect dashboard page using codeigniter

Login Controller:

class Login extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('session');
        $this->load->library('form_validation');
        $this->load->model('login_model');
    }

  public function index() {
        $this->form_validation->set_rules('username', 'UserName', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');
        if ($this->form_validation->run() == FALSE) {
            $this->load->view('admin/login');
        } else {
            $result = $this->login_model->select_login($_POST);   //check username and password
            if ($result) {
                $user = array(
                    'id' => $result['id'],
                    'username' => $result['username'],
                    'password' => $result['password']
                );
                $this->session->set_userdata($user);
                redirect('admin/Dashboard');
            } else {
                $data['msg'] = 'Invalid UserName and Password';
                $this->load->view('admin/login', $data);
            }
        }
    }

}

Dashboard Controller:

class Dashboard extends CI_Controller {

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

    public function index() {
        $session_id = $this->session->userdata('id');
        if (!empty($session_id)) {
            $data = array(
                'page_title' => 'Dashboard',
                'page_name' => 'dashboard/dashboard',
                'admin_username' => $this->session->userdata('username')
            );
            $this->load->view('admin/template', $data);
        } else {
            redirect('admin/Login');
        }
    }
}

Dashboard View:

    <html>
    <body>
    <h1>Helllo Admin</h1>
    </body>
</html>

Question

How can I redirect my Dashboard if I'm already logged in?

what is admin in "redirect('admin/Dashboard');". Did you made any changes to url in route file?

When you do this

if ($result) {
            $user = array(
                'id' => $result['id'],
                'username' => $result['username'],
                'password' => $result['password']
            );
            $this->session->set_userdata($user);
            redirect('admin/Dashboard');

You are setting your password in your session cookie. All you really need to do is set your username. You should also do something like this in Login function index()

 if(result){
   $user = array(
        'name'         => $result['username]',
        'is_logged_in' => TRUE //add this to your session data
      );
      $this->session->set_userdata($user);
 }

Then create a method in whatever controller you are using

public function is_logged_in()
{
    $is_logged_in = $this->session->userdata('is_logged_in');
    if (!isset($is_logged_in) || $is_logged_in != true)
    {
      redirect("admin/Login");
    }
}

When you are in your Dashboard you can use $this->is_logged_in(); to make sure no one else gets into your Dashboard or forms. Use it at the top of your methods for forms or just getting into your dashboard Then use something like this on your pages

if($this->session->userdata('username') == true){
echo "Hello " .  $this->session->userdata('username'). nbs(3),    anchor("admin/Dashboard", " Go To Dashboard "); //this is all you need to get to your Dashboard if you are logged in.
}else {
echo " "; //doesnt show up if not logged in
}?>

That acts as a link on your pages that only shows up if you are logged in. At the top of all your Dashboard controller methods add the $is_logged_in();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM