php的一个登录的类 [推荐]
2018-09-07 13:41
PHP代码:
<?
/*
*名称:CnkknDPHPLoginClass
*描述:PHP用于登录的类,基于MySQL
*作者:DanielKing,
*日期:2003/8/25
*/
classLogin
{
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=mysqlerror;//mysql出错提示
var$err_username=usernameinvalid;//用户名无效提示
var$err_user=userinvalid;//用户无效提示(被封禁)
var$err_password=passworderror;//密码错误提示
var$err;//出错提示
var$errorreport=false;//显示错误
functionLogin($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;
}
}
functionisLoggedin()//判断是否登录
{
if(isset($_COOKIE[sid]))//如果cookie中保存有sid
{
session_id($_COOKIE[sid]);
session_start();
$this->username=$_SESSION[username];
$this->userid=$_SESSION[userid];
$this->userlevel=$_SESSION[userlevel];
returntrue;
}
else//如果cookie中未保存sid,则直接检查session
{
session_start();
if(isset($_SESSION[username]))
returntrue;
}
returnfalse;
}
functionuserAuth($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;
returnfalse;
}
elseif(md5($userpass)==$row[userpass])//密码匹配
{
$this->userid=$row[id];
$this->userlevel=$row[userlevel];
returntrue;
}
else//密码不匹配
{
$this->errReport($this->err_password);
$this->err=$this->err_password;
returnfalse;
}
}
else//没有找到此用户
{
$this->errReport($this->err_username);
$this->err=$this->err_username;
returnfalse;
}
}
functionsetSession()//置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(setcookiefailed);
}
else
setcookie(sid,,time()0);//清除cookie中的sid
}
functionuserLogout()//用户注销
{
session_start();
unset($_SESSION[username]);//清除session中的username
if(setcookie(sid,,time()0))
//清除cookie中的sid
returntrue;
else
returnfalse;
}
functionerrReport($str)//报错
{
if($this->error_report)
echoERROR:$str;
}
}
?>
mysql中表的结构
代码:
CREATETABLE`account`(
`id`bigint(20)NOTNULLauto_increment,
`username`varchar(255)NOTNULLdefault,
`userpass`varchar(255)NOTNULLdefault,
`banned`tinyint(1)NOTNULLdefault0,
`userlevel`tinyint(4)NOTNULLdefault0,
PRIMARYKEY(`id`)
)
使用一例
PHP代码:
<?
include../myclasses/Login.php;
$dbserv=localhost;
$dbport=3306;
$dbuser=root;
$dbpass=123456;
$dbname=test;
$login=newLogin($dbserv,$dbport,$dbuser,$dbpass,$dbname);
$login->error_report=true;
$login->cookietime=3600*24*30;
if($login->isLoggedin())
{
echo$login->username.hasareadyloggedin;
}
elseif($login->userAuth(danielking,1234))
{
echologinsuccessfully;
$login->setSession();
}
echo<p>...</p>;
/*
if($login->userLogout())
echologgedout;
else
echologoutfailed;
*/
?>
下一篇:PHP中实现进程间通讯