简单的登录类,没有把登录和数据库查询分开
复制代码 代码如下:
/*
* 例子
*
* $Auth=new Auth();
* $Auth->login("123@123.com","123");
* $Auth->logout();
* echo $r->init();
*
**/
验证登陆类
复制代码 代码如下:
<"http://my.oschina.net/xinger">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'])"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]) "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'];
}
}
?>
以上就是本文的全部内容了,希望大家能够喜欢。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
更新日志
- 陈洁仪.1994-心痛【立得唱片】【WAV+CUE】
- 车载必备专用超级选曲《劲爆中文DJ》2CD[WAV+CUE]
- 群星《民歌流淌60年(黑胶CD)》2CD[WAV+分轨]
- 群星《美丽时光》紫银合金AQCD[WAV+CUE]
- 群星《12大巨星畅销精选集》[WAV分轨][1.1G]
- 华语排行冠军曲《百事音乐风云榜》[WAV+CUE][1G]
- 奔驰汽车音乐圣经《醇声典范[白金嗓子] 男极声》音乐传真[WAV+CUE][1G]
- 陈影《如影随形HQ》头版限量[低速原抓WAV+CUE]
- 黄乙玲1996-心痛酒来洗[台湾首版][WAV+CUE]
- 曾庆瑜1990-随风而逝[日本东芝1A1首版][WAV+CUE]
- 群星.2015-凭着爱ADMS2CD【华纳】【WAV+CUE】
- 陈冠希.2017-一只猴子3部曲【摩登天空】【WAV+CUE】
- 金元萱.1996-迷迷糊糊【宝丽金】【WAV+CUE】
- 齐秦《燃烧爱情》马来西亚版[WAV+CUE][1G]
- 动力火车《结伴》2024最新 [FLAC分轨][1G]