问题描述:

How to prevent to direct link access in Code ignitor

E.g.

http://localhost/myapp/admin/displayUser

网友答案:

There is no built in authentication library within CodeIgniter, although there are many well developed third party ones. My preference is for Ion Auth - http://benedmunds.com/ion_auth/.

However if you are simply referring to preventing access to some "private" controller methods, you should add an underscore to the beginning of the method name - this will mean it is not accessible via a url, only via other controller methods:

function _myprivatemethod() {

    return true;
}
网友答案:

This my not help you but it might help someone else.

I did it by creating a hook that checks if the user is logged in . If they are not it redirects them to the home login controller.

if (!$this->session->userdata('logged_in')) {
        redirect('user/login');
}

If you create a Auth_Controller that extends CI Controller then instead of extending CI Controller you can make all your apps extend your new Auth_Controller so they always redirect if the user is not logged in.

网友答案:
if (!$this->session->userdata('logged_in')) {
        redirect('user/login');
}

this will perfectly work.. just remember to load the session library as well.

$this->load->library('session');
相关阅读:
Top