**[权限控制] 利用CI钩子实现权限认证
生活随笔
收集整理的這篇文章主要介紹了
**[权限控制] 利用CI钩子实现权限认证
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
http://codeigniter.org.cn/forums/thread-10877-1-1.html
一直沒找到CI的權(quán)限認(rèn)證擴(kuò)展,以前好像找到過一個老外的擴(kuò)展,不過不怎么好用,現(xiàn)在記不清了,后來仿著jsp firter的方式用CI鉤子寫了一下,感覺還可以,做個小網(wǎng)站,小應(yīng)用足夠了,沒必要搞得太復(fù)雜。看到很多人在網(wǎng)上問,這里把我們的方法分享一下,如果你有更好的實現(xiàn),也請記得分享給我們。^_^
?
http://www.php-chongqing.com/index.php/manage/?
http://www.php-chongqing.com/index.php/manage/article/add?
http://www.php-chongqing.com/index.php/manage/product/delete/1?
http://www.php-chongqing.com/index.php/manage/user
$config['enable_hooks']?=?TRUE;
$hook['post_controller_constructor']?=?array(? ??'class'? ??=>?'ManageAuth',? ??'function'?=>?'auth',? ??'filename'?=>'ManageAuth.php',? ??'filepath'?=>?'hooks');
?
/** 后臺權(quán)限攔截鉤子 * @link http://www.php-chongqing.com * @author bing.peng * */class ManageAuth {
private $CI;
public function __construct()
{
$this->CI = &get_instance();
}
//權(quán)限認(rèn)證
public function auth()
{
$this->CI->load->helper('url');
if ( preg_match("/manage.*/i", uri_string()) ) {
// 需要進(jìn)行權(quán)限檢查的URL
$this->CI->load->library('session');
if( !$this->CI->session->userdata('username') ) {
// 用戶未登陸
redirect('login');
return;
}
}
}
}
?
?
示例中的權(quán)限認(rèn)證很簡單,僅僅只是檢查下session是否存有username,如果有就認(rèn)為用戶已登陸,可以訪問資源,否則就跳轉(zhuǎn)到登陸頁面。注意登陸的url千萬不是能以manage開頭,否則就重向定死循環(huán)了。
轉(zhuǎn)載于:https://www.cnblogs.com/kenshinobiy/p/4414962.html
總結(jié)
以上是生活随笔為你收集整理的**[权限控制] 利用CI钩子实现权限认证的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: setHomeButtonEnabled
- 下一篇: CentOS7 网络安装流程