如果我已经登录,那么使用codeigniter重定向仪表板页面


If I already logged in then redirect dashboard page using codeigniter

登录控制器:

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);
            }
        }
    }
}

仪表板控制器:

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');
        }
    }
}

仪表板视图:

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

问题

如果我已经登录,如何重定向我的仪表板?

"redirect('admin/Dashboard'(;"中的admin是什么。你对路由文件中的url做了任何更改吗?

当您执行此时

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

您正在会话cookie中设置密码。你真正需要做的就是设置你的用户名。您还应该在登录函数index((中执行类似操作

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

然后在使用的任何控制器中创建一个方法

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");
    }
}

当你在你的仪表板上时,你可以使用$this->is_loged_in((;以确保没有其他人进入您的仪表板或表单。在表单方法的顶部使用它,或者直接进入仪表板然后在你的页面上使用类似的东西

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
}?>

它充当页面上的链接,只有当您登录时才会显示。在所有Dashboard控制器方法的顶部添加$is_loged_in((;