Home > php教程 > php手册 > Auth认证类通用认证代码

Auth认证类通用认证代码

WBOY
Release: 2016-06-07 11:40:03
Original
1859 people have browsed it

使用Auth.class.php对系统进行认证,其中频繁用到认证,在前面的基础上做了改进。
登陆成功后,将当前用户的id存入:session('uid',$curUserId);
在Config.php中配置:'NOT_AUTH_MODULE'        =>'Public,Index,Test',    // 默认无需认证模块<br>     'NOT_AUTH_ACTION'        =>'',        // 默认无需认证操作<br>     'SUPERADMIN_UID'=>array(1,2),function authcheck($url,$relation='or'){<br> <br>     $linkArr=explode('/',$url);<br>     if(!(count($linkArr)==2||count($linkArr)==3))return;<br>                     <br>     if(count($linkArr)==2){<br>         $CUR_MODULE_NAME=$linkArr[0];<br>         $CUR_ACTION_NAME=$linkArr[1];<br>     }else{<br>         $CUR_MODULE_NAME=$linkArr[1];<br>         $CUR_ACTION_NAME=$linkArr[2];<br>     }        <br>     $notAuth=in_array($CUR_MODULE_NAME, explode(",", C("NOT_AUTH_MODULE"))) || in_array($CUR_ACTION_NAME, explode(",", C("NOT_AUTH_ACTION")));<br>         <br>     $AUTH_CONFIG=C('AUTH_CONFIG');<br>     if(!$AUTH_CONFIG['AUTH_ON']||$notAuth)return true;<br>     else{<br>         if(!session("?uid"))return false;<br> <br>         if(in_array(session('uid'),C('SUPERADMIN_UID')))return true;<br>         else{<br>             import('ORG.Util.Auth');<br>             $auth=new Auth();<br>             if($auth->check($CUR_MODULE_NAME.'/'.$CUR_ACTION_NAME,session('uid'),$relation))return true;<br>             else return false;<br>         }<br>     }<br> }所有的Action均需要继承CommonAction.class.php,在Common的 _initialize()方法中使用:if(!authcheck(MODULE_NAME.'/'.ACTION_NAME))$this->error('你没有权限',U('Index/index'));在后台中对think_auth_rule数据表中在name字段存入操作的url,如:User/index,User/addRecord......

有不明白的地方有问必答,欢迎大家探讨。

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template