<?
/*
* 名稱:CnkknD PHP Login Class
* 描述:PHP用於登陸的類,基於MySQL
* 做者:Daniel King,cnkknd@163.com
* 日期:2003/8/25
*/
class
Login
{
var $username;
//用戶名
var $userpass;
//密碼
var $userid;
//用戶id
var $userlevel;
//用戶級別
var $authtable="account";
//驗證用數據表
var $usecookie=true;
//使用cookie保存sessionid
var $cookiepath='/';
//cookie路徑
var $cookietime=108000;
//cookie有效時間
var $err_mysql="mysql error";
//mysql出錯提示
var $err_username="username invalid";
//用戶名無效提示
var $err_user="user invalid";
//用戶無效提示(被封禁)
var $err_password="password error";
//密碼錯誤提示
var $err;
//出錯提示
var $errorreport=false;
//顯示錯誤
function Login($dbserv,$dbport,$dbuser,$dbpass,$dbname)
//構造函數,鏈接數據庫
{
if(@mysql_pconnect($dbserv.":".$dbport,$dbuser,$dbpass
))
{
mysql_select_db($dbname
);
}
else
{
$this->errReport($this->err_mysql
);
$this->err=$this->err_mysql
;
}
}
function isLoggedin()
//判斷是否登陸
{
if(isset($_COOKIE['sid']))
//若是cookie中保存有sid
{
session_id($_COOKIE['sid'
]);
session_start
();
$this->username=$_SESSION['username'
];
$this->userid=$_SESSION['userid'
];
$this->userlevel=$_SESSION['userlevel'
];
return true
;
}
else
//若是cookie中未保存sid,則直接檢查session
{
session_start
();
if(isset($_SESSION['username'
]))
return true
;
}
return false
;
}
function userAuth($username,$userpass)
//用戶認證
{
$this->username=$username
;
$this->userpass=$userpass
;
$query="select * from `".$this->authtable."` where `username`='$username';"
;
$result=mysql_query($query
);
if(mysql_num_rows($result)!=0)
//找到此用戶
{
$row=mysql_fetch_array($result
);
if($row['bannd']==1)
//此用戶被封禁
{
$this->errReport($this->err_user
);
$this->err=$this->err_user
;
return false
;
}
elseif(md5($userpass)==$row['userpass'])
//密碼匹配
{
$this->userid=$row['id'
];
$this->userlevel=$row['userlevel'
];
return true
;
}
else
//密碼不匹配
{
$this->errReport($this->err_password
);
$this->err=$this->err_password
;
return false
;
}
}
else
//沒有找到此用戶
{
$this->errReport($this->err_username
);
$this->err=$this->err_username
;
return false
;
}
}
function setSession()
//置session
{
$sid=uniqid('sid');
//生成sid
session_id($sid
);
session_start
();
$_SESSION['username']=$this->username;
//給session變量賦值
$_SESSION['userid']=$this->userid;
//..
$_SESSION['userlevel']=$this->userlevel;
//..
if($this->use_cookie)
//若是使用cookie保存sid
{
if(!setcookie('sid',$sid,time()+$this->cookietime,$this->cookiepath
))
$this->errReport("set cookie failed"
);
}
else
setcookie('sid','',time()-3600);
//清除cookie中的sid
}
function userLogout()
//用戶註銷
{
session_start
();
unset($_SESSION['username']);
//清除session中的username
if(setcookie('sid','',time()-3600
))
//清除cookie中的sid
return true
;
else
return false
;
}
function errReport($str)
//報錯
{
if($this->error_report
)
echo "ERROR: $str"
;
}
}
?>