代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
PHP
】
验证登陆类
作者:
Hardy
/ 发布于
2015/10/13
/
609
用法 /* * 例子 * * $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 * **/ classAuth { //外部设置 //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; publicfunction__construct($config=array()){ $this->set($config); $this->pre_username=sha1(md5($this->pre.'username')); $this->pre_password=sha1(md5($this->pre.'password')); } publicfunctionset($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']:'-'; } // publicfunctioninit(){ $this->user_id = 0; $this->username =$this->guest_name; $this->ok = false; if(!$this->check_session()){ $this->check_cookie(); } return$this->ok; } //验证SESSION privatefunctioncheck_session(){ if(!empty($_SESSION[$this->pre_username])&&!empty($_SESSION[$this->pre_password])){ return$this->check($_SESSION[$this->pre_username],$_SESSION[$this->pre_password]); }else{ returnfalse; } } //验证COOKIE privatefunctioncheck_cookie(){ if(!empty($_COOKIE[$this->pre_username])&&!empty($_COOKIE[$this->pre_password])){ return$this->check($_COOKIE[$this->pre_username],$_COOKIE[$this->pre_password]); }else{ returnfalse; } } //登陆 publicfunctionlogin($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); returntrue; } returnfalse; } //验证 privatefunctioncheck($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())returnfalse; 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']; returntrue; } $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); returntrue; } } returnfalse; } //退出 publicfunctionlogout(){ $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); } //加密 publicfunctionencrypt($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)) { returnsubstr($result,8); } else { return''; } } else { returnstr_replace('=','',base64_encode($result)); } } publicfunctionget_ip() { return$_SERVER['REMOTE_ADDR']; } } ?> * 例子 * * $Auth=new Auth(); * $Auth->login("123@123.com","123"); * $Auth->logout(); * echo $r->init(); * **/
试试其它关键字
验证登陆类
同语言下
.
用net匹配并替换iOS标准的emoji表情符号
.
处理带Emoji表情的的字符串
.
获取微信昵称时 过滤特殊字符
.
通过判断上传文件的头字符来判断文件的类型
.
模拟百度URL加密解密算法
.
以太坊检查地址是否合法
.
实现crontab解析类
.
获取每个月的开始和结束时间
.
图片上传工具类
.
APP手机应用信息采集
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Hardy
贡献的其它代码
(
26
)
.
jQuery地区三级联动选择输入
.
在数据库中所有表所有栏位 找出匹配某个值的 脚本
.
伪造客户端获取页面
.
使用Google Chart API生成二维码以及制作点子名片
.
汉字转换为拼音
.
采集远程图片保存本地
.
通过气象局开放API查询指定城市(区县)天气
.
获取文件创建时间与修改时间
.
验证登陆类
.
发邮件
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3