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 相关文章推荐
ThinkPHP的截取字符串函数无法显示省略号的解决方法
Jun 25 PHP
解决phpcms更换javascript的幻灯片代码调用图片问题
Dec 26 PHP
PHP保存session到memcache服务器的方法
Jan 19 PHP
php判断手机浏览还是web浏览,并执行相应的动作简单实例
Jul 28 PHP
php curl中gzip的压缩性能测试实例分析
Nov 08 PHP
php分页查询的简单实现代码
Mar 14 PHP
Yii框架实现记录日志到自定义文件的方法
May 23 PHP
PHP框架自动加载类文件原理详解
Jun 06 PHP
PHP排序二叉树基本功能实现方法示例
May 26 PHP
小程序微信退款功能实现方法详解【基于thinkPHP】
May 05 PHP
laravel 之 Eloquent 模型修改器和序列化示例
Oct 17 PHP
Thinkphp5框架异常处理操作实例分析
Jun 03 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使用simplexml_load_file加载XML文件并显示XML的方法
2015/03/19 PHP
php中namespace use用法实例分析
2016/01/22 PHP
PHP开发之归档格式phar文件概念与用法详解【创建,使用,解包还原提取】
2017/11/17 PHP
php转换上传word文件为PDF的方法【基于COM组件】
2019/06/10 PHP
使用composer安装使用thinkphp6.0框架问题【视频教程】
2019/10/01 PHP
JQuery插件开发示例代码
2013/11/06 Javascript
jQuery中offsetParent()方法用法实例
2015/01/19 Javascript
jQuery插件jRumble实现网页元素抖动
2015/06/05 Javascript
jQuery中extend函数详解
2015/07/13 Javascript
写给小白的JavaScript引擎指南
2015/12/04 Javascript
JS区分浏览器页面是刷新还是关闭
2016/04/17 Javascript
jQuery Mobile页面返回不需要重新get
2016/04/26 Javascript
d3.js入门教程之数据绑定详解
2017/04/28 Javascript
iview tabs 顶部导航栏和模块切换栏的示例代码
2019/03/04 Javascript
解决Layui当中的导航条动态添加后渲染失败的问题
2019/09/25 Javascript
如何通过JS实现日历简单算法
2020/10/14 Javascript
微信小程序实现星星评分效果
2020/11/01 Javascript
[37:23]DOTA2上海特级锦标赛主赛事日 - 3 胜者组第二轮#2Secret VS EG第二局
2016/03/04 DOTA
[01:12:27]EG vs Secret 2018国际邀请赛淘汰赛BO3 第二场 8.22
2018/08/23 DOTA
python目录操作之python遍历文件夹后将结果存储为xml
2014/01/27 Python
深入理解Python中的内置常量
2017/05/20 Python
Python3 适合初学者学习的银行账户登录系统实例
2017/08/08 Python
简单实现Python爬取网络图片
2018/04/01 Python
在python带权重的列表中随机取值的方法
2019/01/23 Python
PyQt5固定窗口大小的方法
2019/06/18 Python
python如何实现代码检查
2019/06/28 Python
python如何获取列表中每个元素的下标位置
2019/07/01 Python
python提取xml里面的链接源码详解
2019/10/15 Python
Python 调用有道翻译接口实现翻译
2020/03/02 Python
Pandas中DataFrame基本函数整理(小结)
2020/07/20 Python
Python 带星号(* 或 **)的函数参数详解
2021/02/23 Python
实习评语大全
2014/04/26 职场文书
暑期社会实践先进个人主要事迹
2014/05/22 职场文书
激励口号大全
2014/06/17 职场文书
解放思想大讨论活动总结
2015/05/09 职场文书
业务员管理制度范本
2015/08/06 职场文书