开发者

How to prevent to direct link access in Code igniter

How to prevent to direct link access in Code igniter

E.g.

http://localh开发者_开发百科ost/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;
}


Create a library as authenticate.php having following code into it and add that library in autoload.php

class Authenticate
{
    function Authenticate()
    {
        $CI = & get_instance(); 
        if ($CI->session->userdata('USERID') == "")
        {
            $redirectlink = 'contoller function path to your login page';
            redirect($redirectlink);   
            exit;
        }
    }
}


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜