use ion auth authentication for codeigniter in another controller

后端 未结 3 1880
长发绾君心
长发绾君心 2021-01-22 09:03

I am trying to build a web application with codeigniter. I have installed Ion Auth as my authentication model.

The default Auth.php controller authenticates the user and

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-22 09:33

    obviously the echo $this->session->userdata('username'); does not work as the new controller has no knowledge of the auth controller session.

    Eh...if the session library is loaded, then yes...the controller calling it will be able to access the session variable $username.

    The way we handle this is to create a new controller parent class like MY_Controller in the application/core directory. This class loads common libraries/packages (like session and ion_auth). You could also autoload the libraries and helpers.

    Since ion_auth stores all of the user profile data in a session var, all you need (on subsequent, non-authenticated) pages is the session lib to retrieve session data about the logged in user.

    You really should check for their auth status though, and fail gracefully:

    if (!$this->ion_auth->logged_in()) {
        // echo a login link
    } else {
        // echo session var for username
    }
    

    Something like that...

提交回复
热议问题