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 相关文章推荐
浅谈PHP语法(1)
Oct 09 PHP
一个程序下载的管理程序(三)
Oct 09 PHP
社区(php&amp;&amp;mysql)六
Oct 09 PHP
通俗易懂的php防注入代码
Apr 07 PHP
基于PHP Web开发MVC框架的Smarty使用说明
Apr 19 PHP
PHP无限分类(树形类)的深入分析
Jun 02 PHP
PHP之短标签开启设置
Jun 17 PHP
PHP中array_merge和array相加的区别分析
Jun 17 PHP
php中使用session_set_save_handler()函数把session保存到MySQL数据库实例
Nov 06 PHP
PHPExcel简单读取excel文件示例
May 26 PHP
php中preg_replace正则替换用法分析【一次替换多个值】
Jan 17 PHP
php实现的三个常用加密解密功能函数示例
Nov 06 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
高分R级DC动画剧《哈莉·奎茵》第二季正式预告首发
2020/04/09 欧美动漫
功能齐全的PHP发送邮件类代码附详细说明
2008/07/10 PHP
PHP中读取照片exif信息的方法
2014/08/20 PHP
浅谈thinkphp的实例化模型
2015/01/04 PHP
php时间函数用法分析
2016/05/28 PHP
PHP实现websocket通信的方法示例
2018/08/28 PHP
浅谈PHP5.6 与 PHP7.0 区别
2019/10/09 PHP
JS中style属性
2006/10/11 Javascript
javascript full screen 全屏显示页面元素的方法
2013/09/27 Javascript
jquery validate在ie8下的bug解决方法
2013/11/13 Javascript
JavaScript中伪协议 javascript:使用探讨
2014/07/18 Javascript
jQuery+Ajax实现无刷新操作
2016/01/04 Javascript
详解JS-- 浮点数运算处理
2016/11/28 Javascript
javascript实现一个网页加载进度loading
2017/01/04 Javascript
分析JS中this引发的bug
2017/12/12 Javascript
微信小程序开发问题之wx.previewImage
2018/12/25 Javascript
elementUI多选框反选的实现代码
2019/04/03 Javascript
使用Vue生成动态表单
2019/11/26 Javascript
JavaScript大数相加相乘的实现方法实例
2020/10/18 Javascript
基于Python os模块常用命令介绍
2017/11/03 Python
将Pytorch模型从CPU转换成GPU的实现方法
2019/08/19 Python
python flask中动态URL规则详解
2019/11/22 Python
Python 3 使用Pillow生成漂亮的分形树图片
2019/12/24 Python
Python爬虫库BeautifulSoup获取对象(标签)名,属性,内容,注释
2020/01/25 Python
python实现发送带附件的邮件代码分享
2020/09/22 Python
python多线程和多进程关系详解
2020/12/14 Python
基于CSS3的CSS 多栏(Multi-column)实现瀑布流源码分享
2014/06/11 HTML / CSS
婴儿地球:Baby Earth
2018/12/25 全球购物
开业主持词
2014/03/21 职场文书
2014年党员创先争优承诺书
2014/05/29 职场文书
医学生求职自荐书
2014/06/12 职场文书
教师工作表现评语
2014/12/31 职场文书
学校教学工作总结2015
2015/05/19 职场文书
关于践行三严三实的心得体会
2016/01/05 职场文书
python-for x in range的用法(注意要点、细节)
2021/05/10 Python
SQL Server作业失败:无法确定所有者是否有服务器访问权限的解决方法
2021/06/30 SQL Server