VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • PHP 验证登陆类分享

本文给大家介绍的是用php实现的用户登录与验证的一段代码,没有把登录和数据库查询分开,有需要的朋友,可以参考学习下。

简单的登录类,没有把登录和数据库查询分开,代码如下:

  1. /* 
  2.  *   例子 
  3.  * 
  4.  *  $Auth=new Auth(); 
  5.  *  $Auth->login("123@123.com","123"); 
  6.  *  $Auth->logout(); 
  7.  *  echo $r->init();     
  8.  * 
  9. **/ 

验证登陆类,代码如下:

  1. <?php 
  2. /* 
  3.  * 
  4.  * @ID:      验证登陆类 
  5.  * 
  6.  * @class:   Auth.class.php 
  7.  * 
  8.  * @auther:  欣儿 
  9.  * 
  10.  * @time:    2015/03/12 
  11.  * 
  12.  * @web:     http://my.oschina.net/xinger 
  13.  * 
  14. **/ 
  15. class Auth { 
  16.     //外部设置 
  17.     //cookie设置 
  18.     var $cookie_time;//         7200 
  19.     var $cookie_where;//        '/' 
  20.     var $cookie_domain;//       'yourweb.com' 
  21.     var $cookie_secure;//       1和0 
  22.     //数据库设置     
  23.     var $select_uid;//          'uid' 
  24.     var $select_table;//        'user' 
  25.     var $select_usersname;//    'email' 
  26.     var $select_password;//     'password' 
  27.     //盐 
  28.     var $salt;//                "12332" 
  29.     var $guest_name;//          'Guest' 
  30.     //用户获取值 
  31.     var $user_id
  32.     var $username
  33.     var $ok
  34.     var $pre;//                 'auth_' 
  35.     var $depr;//                '-' 
  36.     //内部变量 
  37.     private $pre_username
  38.     private $pre_password
  39.     public function __construct($config=array()){ 
  40.         $this->set($config); 
  41.         $this->pre_username=sha1(md5($this->pre.'username')); 
  42.         $this->pre_password=sha1(md5($this->pre.'password')); 
  43.     } 
  44.     public function set($config){ 
  45.         $this->cookie_time       = isset($config['cookie_time'])?$config['cookie_time']: 7200; 
  46.         $this->cookie_where      = isset($config['cookie_where'])?$config['cookie_where']:'/'
  47.         $this->cookie_domain = isset($config['cookie_domain'])?$config['cookie_domain']:''
  48.         $this->cookie_secure = isset($config['cookie_secure'])?$config['cookie_secure']:''
  49.         $this->select_uid        = isset($config['select_uid'])?$config['select_uid']:'uid'
  50.         $this->select_table      = isset($config['select_table'])?$config['select_table']:'table'
  51.         $this->select_usersname  = isset($config['select_usersname'])?$config['select_usersname']:'user_name'
  52.         $this->select_password   = isset($config['select_password'])?$config['select_password']:'password'
  53.         $this->salt              = isset($config['salt'])?$config['salt']:'sghsdghsdg';// 
  54.         $this->guest_name        = isset($config['guest_name'])?$config['guest_name']:'Guest';// 
  55.         $this->pre               = isset($config['auth'])?$config['auth']:'auth_'
  56.         $this->depr              = isset($config['depr'])?$config['depr']:'-'
  57.     } 
  58.     // 
  59.     public function init(){ 
  60.         $this->user_id       = 0; 
  61.         $this->username      = $this->guest_name; 
  62.         $this->ok            = false; 
  63.         if(!$this->check_session()){ 
  64.             $this->check_cookie(); 
  65.         } 
  66.         return $this->ok; 
  67.     } 
  68.     //验证SESSION 
  69.     private function check_session(){ 
  70.         if(!emptyempty($_SESSION[$this->pre_username])&&!emptyempty($_SESSION[$this->pre_password])){ 
  71.             return $this->check($_SESSION[$this->pre_username],$_SESSION[$this->pre_password]); 
  72.         } else { 
  73.             return false; 
  74.         } 
  75.     } 
  76.     //验证COOKIE 
  77.     private function check_cookie(){ 
  78.         if(!emptyempty($_COOKIE[$this->pre_username])&&!emptyempty($_COOKIE[$this->pre_password])){ 
  79.             return $this->check($_COOKIE[$this->pre_username],$_COOKIE[$this->pre_password]); 
  80.         } else { 
  81.             return false; 
  82.         } 
  83.     } 
  84.     //登陆 
  85.     public function login($username,$password){ 
  86.         $sql    = "select ".$this->select_uid." from ".$this->select_table." where ".$this->select_usersname."='$username' and ".$this->select_password."='$password'"
  87.         $result = mysql_query($sql); 
  88.         $rows   = mysql_num_rows($sql); 
  89.         if($rows==1){ 
  90.             $this->user_id   = mysql_result($result,0,0); 
  91.             $this->username  = $username
  92.             $this->ok        = true; 
  93.             $username   = $username.$this->depr.$this->get_ip(); 
  94.             $user_name  = $this->encrypt($username,'E',$this->salt); 
  95.             $_SESSION[$this->pre_username]=$user_name
  96.             $_SESSION[$this->pre_password]=md5(md5($password,$this->salt)); 
  97.             setcookie($this->pre_username,$user_name,time()+$this->cookie_time,$this->cookie_where,$this->cookie_domain,$this->cookie_secure); 
  98.             setcookie($this->pre_password,md5(md5($password,$this->salt)),time()+$this->cookie_time,$this->cookie_where,$this->cookie_domain,$this->cookie_secure); 
  99.             return true; 
  100.         } 
  101.         return false; 
  102.     } 
  103.     //验证 
  104.     private function check($username,$password){ 
  105.         $user_name  = $this->encrypt($username,'D',$this->salt); 
  106.         $name       = explode($this->depr, $user_name); 
  107.         $username   = $name[0]; 
  108.         $ip         = isset($name[1]) ? $name[1] : NULL; 
  109.         if($ip !== $this->get_ip()) return false; 
  110.         static $vars = array(); 
  111.         if(!emptyempty($vars)&&is_array($vars)&&isset($vars[$username.$password])){ 
  112.             $this->user_id   = $vars['user_id']; 
  113.             $this->username  = $vars['username']; 
  114.             $this->ok        = $vars['ok']; 
  115.             return true; 
  116.         } 
  117.         $sql    = "select ".$this->select_uid.",".$this->select_password." from ".$this->select_table." where ".$this->select_usersname."='$username'"
  118.         $query  = mysql_query($sql); 
  119.         $result = mysql_fetch_array($query); 
  120.         $row    = mysql_num_rows($sql); 
  121.         if($row == 1){ 
  122.             $db_password=$result[$this->select_password]; 
  123.             if(md5(md5($db_password,$this->salt)) == $password){ 
  124.                 $this->user_id   = $vars['user_id']  = $result[$this->select_uid]; 
  125.                 $this->username  = $vars['username'] = $username
  126.                 $this->ok        = $vars['ok']       = true; 
  127.                 $vars[$username.$password]          = md5($username.$password); 
  128.                 return true; 
  129.             } 
  130.         } 
  131.         return false; 
  132.     } 
  133.     //退出 
  134.     public function logout(){ 
  135.         $this->user_id       = 0; 
  136.         $this->username      = $this->guest_name; 
  137.         $this->ok            = false; 
  138.         $_SESSION[$this->pre_username]=""
  139.         $_SESSION[$this->pre_password]=""
  140.         setcookie($this->pre_username,"",time()-$this->cookie_time,$this->cookie_where,$this->cookie_domain,$this->cookie_secure); 
  141.         setcookie($this->pre_password,"",time()-$this->cookie_time,$this->cookie_where,$this->cookie_domain,$this->cookie_secure); 
  142.     }   
  143.     //加密 
  144.     public function encrypt($string,$operation,$key='') { 
  145.         $key=md5($key); 
  146.         $key_length=strlen($key); 
  147.         $string=$operation=='D'?base64_decode($string):substr(md5($string.$key),0,8).$string
  148.         $string_length=strlen($string); 
  149.         $rndkey=$box=array(); 
  150.         $result=''
  151.         for($i=0;$i<=255;$i++) 
  152.         { 
  153.             $rndkey[$i]=ord($key[$i%$key_length]); 
  154.             $box[$i]=$i
  155.         } 
  156.         for($j=$i=0;$i<256;$i++) 
  157.         { 
  158.             $j=($j+$box[$i]+$rndkey[$i])%256; 
  159.             $tmp=$box[$i]; 
  160.             $box[$i]=$box[$j]; 
  161.             $box[$j]=$tmp
  162.         } 
  163.         for($a=$j=$i=0;$i<$string_length;$i++) 
  164.         { 
  165.             $a=($a+1)%256; 
  166.             $j=($j+$box[$a])%256; 
  167.             $tmp=$box[$a]; 
  168.             $box[$a]=$box[$j]; 
  169.             $box[$j]=$tmp
  170.             $result.=chr(ord($string[$i])^($box[($box[$a]+$box[$j])%256])); 
  171.         } 
  172.         if($operation=='D'
  173.         { 
  174.             if(substr($result,0,8)==substr(md5(substr($result,8).$key),0,8)) 
  175.             { 
  176.                 return substr($result,8); 
  177.             } 
  178.             else 
  179.             { 
  180.                 return''
  181.             } 
  182.         } 
  183.         else 
  184.         { 
  185.             return str_replace('=','',base64_encode($result)); 
  186.         } 
  187.     } 
  188.     public function get_ip() { 
  189.         return $_SERVER['REMOTE_ADDR']; 
  190.     } 

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

 



出处:http://www.phpfensi.com/php/20210516/15429.html


相关教程