<?php
class
Login
{
var
$username
;
var
$userpass
;
var
$userid
;
var
$userlevel
;
var
$authtable
=”account”;
var
$usecookie
=true;
var
$cookiepath
=’/';
var
$cookietime
=108000;
var
$err_mysql
=”mysql error”;
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'
]))
{
session_id(
$_COOKIE
[
'sid'
]);
session_start();
$this
->username=
$_SESSION
[
'username'
];
$this
->userid=
$_SESSION
[
'userid'
];
$this
->userlevel=
$_SESSION
[
'userlevel'
];
return
true;
}
else
{
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()
{
$sid
=uniqid(’sid’);
session_id(
$sid
);
session_start();
$_SESSION
[
'username'
]=
$this
->username;
$_SESSION
[
'userid'
]=
$this
->userid;
$_SESSION
[
'userlevel'
]=
$this
->userlevel;
if
(
$this
->use_cookie)
{
if
(!setcookie(’sid’,
$sid
,time()+
$this
->cookietime,
$this
->cookiepath))
$this
->errReport(”set cookie failed”);
}
else
setcookie(’sid’,”,time()-3600);
}
function
userLogout()
{
session_start();
unset(
$_SESSION
[
'username'
]);
if
(setcookie(’sid’,”,time()-3600))
return
true;
else
return
false;
}
function
errReport(
$str
)
{
if
(
$this
->error_report)
echo
“ERROR:
$str
”;
}
}
?>