php的一个登录的类 [推荐]
程序员文章站
2022-11-25 21:49:53
php代码: /* * 名称:cnkknd php login class * 描述:php用于登录的类,基于...
php代码:
<?
/*
* 名称: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";
}
}
?>
mysql中表的结构
代码:
create table `account` (
`id` bigint(20) not null auto_increment,
`username` varchar(255) not null default '',
`userpass` varchar(255) not null default '',
`banned` tinyint(1) not null default '0',
`userlevel` tinyint(4) not null default '0',
primary key (`id`)
)
使用一例
php代码:
<?
include "../myclasses/login.php";
$dbserv="localhost";
$dbport="3306";
$dbuser="root";
$dbpass="123456";
$dbname="test";
$login=new login($dbserv,$dbport,$dbuser,$dbpass,$dbname);
$login->error_report=true;
$login->cookietime=3600*24*30;
if($login->isloggedin())
{
echo $login->username." has aready logged in";
}
elseif($login->userauth("danielking","1234"))
{
echo "login successfully";
$login->setsession();
}
echo "<p>...</p>";
/*
if($login->userlogout())
echo "logged out";
else
echo "logout failed";
*/
?>
<?
/*
* 名称: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";
}
}
?>
mysql中表的结构
代码:
create table `account` (
`id` bigint(20) not null auto_increment,
`username` varchar(255) not null default '',
`userpass` varchar(255) not null default '',
`banned` tinyint(1) not null default '0',
`userlevel` tinyint(4) not null default '0',
primary key (`id`)
)
使用一例
php代码:
<?
include "../myclasses/login.php";
$dbserv="localhost";
$dbport="3306";
$dbuser="root";
$dbpass="123456";
$dbname="test";
$login=new login($dbserv,$dbport,$dbuser,$dbpass,$dbname);
$login->error_report=true;
$login->cookietime=3600*24*30;
if($login->isloggedin())
{
echo $login->username." has aready logged in";
}
elseif($login->userauth("danielking","1234"))
{
echo "login successfully";
$login->setsession();
}
echo "<p>...</p>";
/*
if($login->userlogout())
echo "logged out";
else
echo "logout failed";
*/
?>
上一篇: 这位同学很有想法!