您的位置:首页 > 编程语言 > PHP开发

简单的php登陆类及后台权限的控制

2012-11-05 17:03 375 查看
<?php

/**

 * 登陆类

 */

class login extends action{

    /**

     * 用户登录

     * $username为提交的用户名

     * $password为提交的密码

     * $result为查询数据库后的结果

     * $url为登录成功后要跳转的页面

     */

    function tologin($username,$password,$result,$url){

     $user = is_array($result);

        $pass = $user ? md5($password)==$result['password'] : FALSE;

        if($pass){

         $_SESSION['uid']       = $result['id'];

         $_SESSION['username']  = $result['username'];

         $_SESSION['user_shell'] = md5($result[0]['username'].$result[0]['password']);

         echo "<script>location.href='".$url."'</script>";

        }else{

   echo "<script>alert('用户名或密码错误!');</script>";

         //session_destroy();

         unset($_SESSION['uid']);

   unset($_SESSION['username']);

         unset($_SESSION['user_shell']);

        }

 }

//=====================================================

 /**

  * 用户权限判断

  * $uid为用户名

  * $shell为加密的文字

  * $result为数据库执行的结果

  * $m_id 为下级权限

  * $url为无权访问后跳转的页面

  */

 function islogin($uid,$shell,$result,$m_id,$url){

     $user = is_array($result);

        $pass = $user ? $shell==md5($result[0]['password']) : FALSE;

        if($shell){

         if($result['m_id']<=$m_id){

    $re = $result[0];

          return $re;

         }else{

          echo "<script>alert('您的权限不足!');location.href='".$url."'</script>";

          exit();

         }

        }else{

         echo "<script>alert('您无权访问该页!');</script>";

         exit();

        }

    }

//=======================================================

   /**

    * 退出登陆

    * $uid = $_SESSION['uid'];

    * $shell = $_SESSION['user_shell'];

    * $result = $db->select_all("admin","*","`id`='".$uid."'");

    * $re = $login -> islogin($uid,$shell,$result);

    * print_r($re);

    */

    function logout($url){

     //session_destroy();

     unset($_SESSION['uid']);  //删除session

  unset($_SESSION['username']);

     unset($_SESSION['user_shell']);

     echo "<script>window.parent.location.href='".$url."'</script>";

    }

//==========================================================

 /**

  * 用户登录超时时间(秒)

  * $long为用户登录多长时间后提示登录超时,单位是秒

  */

 function login_mktime($long='3600'){

  $new_time=mktime();

  $onlinetime=$_SESSION[ontime];

  echo $new_time-$onlinetime;

  if($new_time-$onlinetime>$long){

   echo "<script>alert('登录超时');</script>";

   session_destroy(); //释放session

  }

 }

//=============================================================

 /**

  * 后台通用跳转页面提示

  * $url为要跳转到的页面地址

  */

 function admin_missage($url,$show='操作已成功!'){

  $msg='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>管理区域</title>

    </head>

    <body>

    <div>

      <table width="30%" border="1" align="center" cellpadding="3" cellspacing="0">

        <tr>

         <th align="center" style="background:#ccf">信息提示</th>

       </tr>

        <tr>

         <td><p>'.$show.'<br />

           2秒后返回指定页面!<br />

           如果浏览器无法跳转,<a href="'.$url.'">请点击此处</a></p></td>

       </tr>

    </table>

    </div>

    </body>

    </html>';

    echo $msg;

    exit();

 }

}//end class

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: