PHP 验证登陆类分享


Posted in PHP onMarch 13, 2015

简单的登录类,没有把登录和数据库查询分开

/*

 *   例子

 *

 *  $Auth=new Auth();

 *  $Auth->login("123@123.com","123");

 *  $Auth->logout();

 *  echo $r->init();     

 *

**/

验证登陆类

<?php

/*

 *

 * @ID:      验证登陆类

 *

 * @class:   Auth.class.php

 *

 * @auther:  欣儿

 *

 * @time:    2015/03/12

 *

 * @web:     http://my.oschina.net/xinger

 *

**/

class Auth {

    //外部设置

    //cookie设置

    var $cookie_time;//         7200

    var $cookie_where;//        '/'

    var $cookie_domain;//       'yourweb.com'

    var $cookie_secure;//       1和0

    //数据库设置     

    var $select_uid;//          'uid'

    var $select_table;//        'user'

    var $select_usersname;//    'email'

    var $select_password;//     'password'

    //盐

    var $salt;//                "12332"

    var $guest_name;//          'Guest'

    //用户获取值

    var $user_id;

    var $username;

    var $ok;

    var $pre;//                 'auth_'

    var $depr;//                '-'

    //内部变量

    private $pre_username;

    private $pre_password;

    public function __construct($config=array()){

        $this->set($config);

        $this->pre_username=sha1(md5($this->pre.'username'));

        $this->pre_password=sha1(md5($this->pre.'password'));

    }

    public function set($config){

        $this->cookie_time       = isset($config['cookie_time'])?$config['cookie_time']: 7200;

        $this->cookie_where      = isset($config['cookie_where'])?$config['cookie_where']:'/';

        $this->cookie_domain = isset($config['cookie_domain'])?$config['cookie_domain']:'';

        $this->cookie_secure = isset($config['cookie_secure'])?$config['cookie_secure']:'';

        $this->select_uid        = isset($config['select_uid'])?$config['select_uid']:'uid';

        $this->select_table      = isset($config['select_table'])?$config['select_table']:'table';

        $this->select_usersname  = isset($config['select_usersname'])?$config['select_usersname']:'user_name';

        $this->select_password   = isset($config['select_password'])?$config['select_password']:'password';

        $this->salt              = isset($config['salt'])?$config['salt']:'sghsdghsdg';//

        $this->guest_name        = isset($config['guest_name'])?$config['guest_name']:'Guest';//

        $this->pre               = isset($config['auth'])?$config['auth']:'auth_';

        $this->depr              = isset($config['depr'])?$config['depr']:'-';

    }

    //

    public function init(){ 

        $this->user_id       = 0;

        $this->username      = $this->guest_name;

        $this->ok            = false;

        if(!$this->check_session()){

            $this->check_cookie();

        }

        return $this->ok;

    }

    //验证SESSION

    private function check_session(){

        if(!empty($_SESSION[$this->pre_username])&&!empty($_SESSION[$this->pre_password])){

            return $this->check($_SESSION[$this->pre_username],$_SESSION[$this->pre_password]);

        } else {

            return false;

        }

    }

    //验证COOKIE

    private function check_cookie(){

        if(!empty($_COOKIE[$this->pre_username])&&!empty($_COOKIE[$this->pre_password])){

            return $this->check($_COOKIE[$this->pre_username],$_COOKIE[$this->pre_password]);

        } else {

            return false;

        }

    }

    //登陆

    public function login($username,$password){

        $sql    = "select ".$this->select_uid." from ".$this->select_table." where ".$this->select_usersname."='$username' and ".$this->select_password."='$password'";

        $result = mysql_query($sql);

        $rows   = mysql_num_rows($sql);

        if($rows==1){

            $this->user_id   = mysql_result($result,0,0);

            $this->username  = $username;

            $this->ok        = true;

            $username   = $username.$this->depr.$this->get_ip();

            $user_name  = $this->encrypt($username,'E',$this->salt);

            $_SESSION[$this->pre_username]=$user_name;

            $_SESSION[$this->pre_password]=md5(md5($password,$this->salt));

            setcookie($this->pre_username,$user_name,time()+$this->cookie_time,$this->cookie_where,$this->cookie_domain,$this->cookie_secure);

            setcookie($this->pre_password,md5(md5($password,$this->salt)),time()+$this->cookie_time,$this->cookie_where,$this->cookie_domain,$this->cookie_secure);

            return true;

        }

        return false;

    }

    //验证

    private function check($username,$password){

        $user_name  = $this->encrypt($username,'D',$this->salt);

        $name       = explode($this->depr, $user_name);

        $username   = $name[0];

        $ip         = isset($name[1]) ? $name[1] : NULL;

        if($ip !== $this->get_ip()) return false;

        static $vars = array();

        if(!empty($vars)&&is_array($vars)&&isset($vars[$username.$password])){

            $this->user_id   = $vars['user_id'];

            $this->username  = $vars['username'];

            $this->ok        = $vars['ok'];

            return true;

        }

        $sql    = "select ".$this->select_uid.",".$this->select_password." from ".$this->select_table." where ".$this->select_usersname."='$username'";

        $query  = mysql_query($sql);

        $result = mysql_fetch_array($query);

        $row    = mysql_num_rows($sql);

        if($row == 1){

            $db_password=$result[$this->select_password];

            if(md5(md5($db_password,$this->salt)) == $password){

                $this->user_id   = $vars['user_id']  = $result[$this->select_uid];

                $this->username  = $vars['username'] = $username;

                $this->ok        = $vars['ok']       = true;

                $vars[$username.$password]          = md5($username.$password);

                return true;

            }

        }

        return false;

    }

    //退出

    public function logout(){

        $this->user_id       = 0;

        $this->username      = $this->guest_name;

        $this->ok            = false;

        $_SESSION[$this->pre_username]="";

        $_SESSION[$this->pre_password]="";

        setcookie($this->pre_username,"",time()-$this->cookie_time,$this->cookie_where,$this->cookie_domain,$this->cookie_secure);

        setcookie($this->pre_password,"",time()-$this->cookie_time,$this->cookie_where,$this->cookie_domain,$this->cookie_secure);

    }   

    //加密 

    public function encrypt($string,$operation,$key='') {

        $key=md5($key);

        $key_length=strlen($key);

        $string=$operation=='D'?base64_decode($string):substr(md5($string.$key),0,8).$string;

        $string_length=strlen($string);

        $rndkey=$box=array();

        $result='';

        for($i=0;$i<=255;$i++)

        {

            $rndkey[$i]=ord($key[$i%$key_length]);

            $box[$i]=$i;

        }

        for($j=$i=0;$i<256;$i++)

        {

            $j=($j+$box[$i]+$rndkey[$i])%256;

            $tmp=$box[$i];

            $box[$i]=$box[$j];

            $box[$j]=$tmp;

        }

        for($a=$j=$i=0;$i<$string_length;$i++)

        {

            $a=($a+1)%256;

            $j=($j+$box[$a])%256;

            $tmp=$box[$a];

            $box[$a]=$box[$j];

            $box[$j]=$tmp;

            $result.=chr(ord($string[$i])^($box[($box[$a]+$box[$j])%256]));

        }

        if($operation=='D')

        {

            if(substr($result,0,8)==substr(md5(substr($result,8).$key),0,8))

            {

                return substr($result,8);

            }

            else

            {

                return'';

            }

        }

        else

        {

            return str_replace('=','',base64_encode($result));

        }

    }

    public function get_ip() {

        return $_SERVER['REMOTE_ADDR'];

    }

}

?>

以上就是本文的全部内容了,希望大家能够喜欢。

PHP 相关文章推荐
织梦模板标记简介
Mar 11 PHP
php 更新数据库中断的解决方法
Jun 05 PHP
PHP 简易输出CSV表格文件的方法详解
Jun 20 PHP
php jquery 多文件上传简单实例
Dec 23 PHP
php ci框架中加载css和js文件失败的解决方法
Mar 03 PHP
PHP 如何获取二维数组中某个key的集合
Jun 03 PHP
PHP实现图片压缩的两则实例
Jul 19 PHP
PHP命名空间与自动加载类详解
Sep 04 PHP
PHP后期静态绑定之self::限制实例分析
Dec 21 PHP
实例讲解PHP表单处理
Feb 15 PHP
PHP基于timestamp和nonce实现的防止重放攻击方案分析
Jul 26 PHP
php实现登录页面的简单实例
Sep 29 PHP
php检索或者复制远程文件的方法
Mar 13 #PHP
php通过文件流方式复制文件的方法
Mar 13 #PHP
php通过递归方式复制目录和子目录的方法
Mar 13 #PHP
php不使用copy()函数复制文件的方法
Mar 13 #PHP
php相对当前文件include其它文件的方法
Mar 13 #PHP
php从完整文件路径中分离文件目录和文件名的方法
Mar 13 #PHP
PHP开发框架laravel安装与配置教程
Mar 13 #PHP
You might like
php 过滤器实现代码
2010/08/09 PHP
简单的PHP缓存设计实现代码
2011/09/30 PHP
PHP数组传递是值传递而非引用传递概念纠正
2013/01/31 PHP
深入php处理整数函数的详解
2013/06/09 PHP
Laravel 5框架学习之模型、控制器、视图基础流程
2015/04/08 PHP
PHP简单实现DES加密解密的方法
2016/07/12 PHP
[原创]js与自动伸缩图片 自动缩小图片的多浏览器兼容的方法总结
2007/03/12 Javascript
jQuery 插件 将this下的div轮番显示
2009/04/09 Javascript
jquery 新浪网易的评论块制作
2010/07/01 Javascript
JS 按钮点击触发(兼容IE、火狐)
2013/08/07 Javascript
JavaScript生成的动态下雨背景效果实现方法
2015/02/25 Javascript
AngularJS 让人爱不释手的八种功能
2016/03/23 Javascript
jQuery中设置form表单中action值的实现方法
2016/05/25 Javascript
JavaScript结合Bootstrap仿微信后台多图文界面管理
2016/07/22 Javascript
基于JS如何实现给字符加千分符(65,541,694,158)
2016/08/03 Javascript
Extjs让combobox写起来简洁又漂亮
2017/01/05 Javascript
详解JS中的快速排序与冒泡
2017/01/10 Javascript
JS常用倒计时代码实例总结
2017/02/07 Javascript
Vue之Watcher源码解析(1)
2017/07/19 Javascript
jQuery Easyui Treegrid实现显示checkbox功能
2017/08/08 jQuery
python3序列化与反序列化用法实例
2015/05/26 Python
Python数据操作方法封装类实例
2017/06/23 Python
python好玩的项目—色情图片识别代码分享
2017/11/07 Python
Python简单生成随机数的方法示例
2018/03/31 Python
python向字符串中添加元素的实例方法
2019/06/28 Python
对Python获取屏幕截图的4种方法详解
2019/08/27 Python
浅谈django框架集成swagger以及自定义参数问题
2020/07/07 Python
html5+css3气泡组件的实现
2014/11/21 HTML / CSS
详解CSS3阴影 box-shadow的使用和技巧总结
2016/12/03 HTML / CSS
感恩节活动方案
2014/01/27 职场文书
《母亲的恩情》教学反思
2014/02/13 职场文书
假面舞会策划方案
2014/05/29 职场文书
幼儿园师德师风学习材料
2014/05/29 职场文书
天地会口号
2014/06/17 职场文书
如何利用Python实现一个论文降重工具
2021/07/09 Python
Java SSM配置文件案例详解
2021/08/30 Java/Android