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 一个随机字符串生成代码
May 26 PHP
PHP下对数组进行排序的函数
Aug 08 PHP
php中用加号与用array_merge合并数组的区别深入分析
Jun 03 PHP
php漏洞之跨网站请求伪造与防止伪造方法
Aug 15 PHP
php ckeditor上传图片文件名乱码解决方法
Nov 15 PHP
解决Codeigniter不能上传rar和zip压缩包问题
Mar 07 PHP
PHP中批量生成静态html(命令行下运行PHP)
Apr 19 PHP
php学习笔记之基础知识
Nov 08 PHP
PHP7.0安装笔记整理
Aug 28 PHP
优化WordPress的Google字体以加速国内服务器上的运行
Nov 24 PHP
php从身份证获取性别和出生年月
Feb 09 PHP
PHP Post获取不到非表单数据的问题解决办法
Feb 27 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 setcookie() cannot modify header information 的解决方法
2009/01/09 PHP
PHP实现合并discuz用户
2015/08/05 PHP
详解HTTP Cookie状态管理机制
2016/01/14 PHP
WHOOPS PHP调试库的使用
2017/09/29 PHP
PHP Post获取不到非表单数据的问题解决办法
2018/02/27 PHP
php二维数组按某个键值排序的实例讲解
2019/02/15 PHP
JavaScript 数组循环引起的思考
2010/01/01 Javascript
jquery创建并行对象或者合并对象的实现代码
2012/10/10 Javascript
JQuery实现简单时尚快捷的气泡提示插件
2012/12/20 Javascript
js加载读取内容及显示与隐藏div示例
2014/02/13 Javascript
黑帽seo劫持程序,js劫持搜索引擎代码
2015/09/15 Javascript
JavaScript模拟文件拖选框样式v1.0的实例
2017/08/04 Javascript
利用10行js代码实现上下滚动公告效果
2017/12/08 Javascript
手淘flexible.js框架使用和源代码讲解小结
2018/10/15 Javascript
实例详解带参数的 npm script
2019/05/28 Javascript
中级前端工程师必须要掌握的27个JavaScript 技巧(干货总结)
2019/09/23 Javascript
vue使用swiper实现中间大两边小的轮播图效果
2019/11/24 Javascript
Javascript实现鼠标点击冒泡特效
2019/12/24 Javascript
vue 路由懒加载中给 Webpack Chunks 命名的方法
2020/04/24 Javascript
[01:13]这,就是刀塔
2014/07/16 DOTA
python自动化测试之setUp与tearDown实例
2014/09/28 Python
python使用线程封装的一个简单定时器类实例
2015/05/16 Python
Python爬虫辅助利器PyQuery模块的安装使用攻略
2016/04/24 Python
python中如何使用朴素贝叶斯算法
2017/04/06 Python
Python实现决策树C4.5算法的示例
2018/05/30 Python
使用PyCharm进行远程开发和调试的实现
2019/11/04 Python
Pandas把dataframe或series转换成list的方法
2020/06/14 Python
CSS 说明横向进度条最后显示文字的实现代码
2020/11/10 HTML / CSS
HTML5 语音搜索(淘宝店语音搜素)
2013/01/03 HTML / CSS
video.js支持m3u8格式直播的实现示例
2020/05/20 HTML / CSS
美国饼干礼物和美食甜点购买网站:Cheryl’s
2020/05/28 全球购物
新闻传播专业求职信
2014/07/22 职场文书
护士优质服务演讲稿
2014/08/26 职场文书
2014年商场国庆节活动策划方案
2014/09/16 职场文书
python办公自动化之excel的操作
2021/05/23 Python
Python何绘制带有背景色块的折线图
2022/04/23 Python