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 相关文章推荐
Discuz 5.0 中读取纯真IP数据库函数分析
Mar 16 PHP
php数据结构与算法(PHP描述) 快速排序 quick sort
Jun 21 PHP
php cc攻击代码与防范方法
Oct 18 PHP
那些年我们错过的魔术方法(Magic Methods)
Jan 14 PHP
php中文字符串截取方法实例总结
Sep 30 PHP
php数组添加元素方法小结
Dec 20 PHP
php实现图片局部打马赛克的方法
Feb 11 PHP
php封装好的人民币数值转中文大写类
Dec 20 PHP
解决安装WampServer时提示缺少msvcr110.dll文件的问题
Jul 09 PHP
基于CI(CodeIgniter)框架实现购物车功能的方法
Apr 09 PHP
PHP实现二叉树深度优先遍历(前序、中序、后序)和广度优先遍历(层次)实例详解
Apr 20 PHP
PHP使用 Pear 进行安装和卸载包的方法详解
Jul 08 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中删除变量时unset()和null的区别分析
2011/01/27 PHP
php 数据库字段复用的基本原理与示例
2011/07/22 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(七)
2014/06/23 PHP
php单文件版在线代码编辑器
2015/03/12 PHP
支付宝服务窗API接口开发php版本
2016/07/20 PHP
php操作mongodb封装类与用法实例
2018/09/01 PHP
求解开jscript.encode代码的asp函数
2007/02/28 Javascript
JavaScript图片放大技术(放大镜)实现代码分享
2013/11/14 Javascript
js拖动div 当鼠标移动时整个div也相应的移动
2013/11/21 Javascript
Javascript封装DOMContentLoaded事件实例
2014/06/12 Javascript
Nodejs中读取中文文件编码问题、发送邮件和定时任务实例
2015/01/01 NodeJs
js显示文本框提示文字的方法
2015/05/07 Javascript
javascript实现验证IP地址等相关信息代码
2015/05/10 Javascript
JavaScript模板引擎用法实例
2015/07/10 Javascript
JavaScript File API实现文件上传预览
2016/02/02 Javascript
JS中的hasOwnProperty()和isPrototypeOf()属性实例详解
2016/08/11 Javascript
Bootstrap整体框架之CSS12栅格系统
2016/12/15 Javascript
JavaScript中的工厂函数(推荐)
2017/03/08 Javascript
Grunt针对静态文件的压缩,版本控制打包的实例讲解
2017/09/29 Javascript
element-ui循环显示radio控件信息的方法
2018/08/24 Javascript
vue图片上传本地预览组件使用详解
2019/02/20 Javascript
[09:37]DOTA2卡尔工作室 英雄介绍圣堂刺客篇
2013/06/13 DOTA
用Python操作字符串之rindex()方法的使用
2015/05/19 Python
Python下实现的RSA加密/解密及签名/验证功能示例
2017/07/17 Python
使用python3构建文件传输的方法
2019/02/13 Python
python基础教程之while循环
2019/08/14 Python
Python json读写方式和字典相互转化
2020/04/18 Python
python工具——Mimesis的简单使用教程
2021/01/16 Python
Christys’ Hats官网:英国帽子制造商
2018/11/28 全球购物
香港通票:Hong Kong Pass
2019/02/26 全球购物
HR喜欢的自荐信格式
2013/10/08 职场文书
在校实习生求职信
2014/06/18 职场文书
幼儿园辞职信范文
2015/02/27 职场文书
《青山不老》教学反思
2016/02/22 职场文书
详解TS数字分隔符和更严格的类属性检查
2021/05/06 Javascript
Nginx流量拷贝ngx_http_mirror_module模块使用方法详解
2022/04/07 Servers