<?
/*
* Description: PHP logon class. It is based on MySQL.
*/
Class Login
{
Var $ username; // User Name
Var $ userpass; // Password
Var $ userid; // user ID
Var $ userlevel; // user level
Var $ authtable = "account"; // verify the data table
Var $ usecookie = true; // use cookie to save sessionid
Var $ cookiepath = '/'; // cookie Path
Var $ cookietime = 108000; // cookie Validity Period
Var $ err_mysql = "mysql error"; // mysql error prompt
Var $ err_username = "username invalid"; // error message indicating invalid username
Var $ err_user = "user invalid"; // message indicating invalid user (banned)
Var $ err_password = "password error"; // password error prompt
Var $ err; // error prompt
Var $ errorreport = false; // Display Error
Function Login ($ dbserv, $ dbport, $ dbuser, $ dbpass, $ dbname) // constructor to connect to the database
{
If (@ mysql_pconnect ($ dbserv. ":". $ dbport, $ dbuser, $ dbpass ))
{
Mysql_select_db ($ dbname );
}
Else
{
$ This-> errReport ($ this-> err_mysql );
$ This-> err = $ this-> err_mysql;
}
}
Function isLoggedin () // determines whether to log on.
{
If (isset ($ _ COOKIE ['sid ']) // if the cookie contains sid
{
Session_id ($ _ COOKIE ['sid ']);
Session_start ();
$ This-> username = $ _ SESSION ['username'];
$ This-> userid = $ _ SESSION ['userid'];
$ This-> userlevel = $ _ SESSION ['userlevel'];
Return true;
}
Else // if no sid is saved in the cookie, check the session directly.
{
Session_start ();
If (isset ($ _ SESSION ['username'])
Return true;
}
Return false;
}
Function userAuth ($ username, $ userpass) // User Authentication
{
$ This-> username = $ username;
$ This-> userpass = $ userpass;
$ Query = "select * from '". $ this-> authtable. "'where' username' =' $ username ';";
$ Result = mysql_query ($ query );
If (mysql_num_rows ($ result )! = 0) // find this user
{
$ Row = mysql_fetch_array ($ result );
If ($ row ['bannd'] = 1) // This user is banned
{
$ This-> errReport ($ this-> err_user );
$ This-> err = $ this-> err_user;
Return false;
}
Elseif (MD5 ($ userpass) = $ row ['userpass']) // Password Match
{
$ This-> userid = $ row ['id'];
$ This-> userlevel = $ row ['userlevel'];
Return true;
}
Else // password Mismatch
{
$ This-> errReport ($ this-> err_password );
$ This-> err = $ this-> err_password;
Return false;
}
}
Else // This user is not found
{
$ This-> errReport ($ this-> err_username );
$ This-> err = $ this-> err_username;
Return false;
}
}
Function setSession () // sets the session
{
$ Sid = uniqid ('sid '); // generate sid
Session_id ($ sid );
Session_start ();
$ _ SESSION ['username'] = $ this-> username; // assign a value to the session variable
$ _ SESSION ['userid'] = $ this-> userid ;//..
$ _ SESSION ['userlevel'] = $ this-> userlevel ;//..
If ($ this-> use_cookie) // if you use cookies to save sid
{
If (! Setcookie ('sid ', $ sid, time () + $ this-> cookietime, $ this-> cookiepath ))
$ This-> errReport ("set cookie failed ");
}
Else
Setcookie ('sid ', '', time ()-3600); // clear the sid in the cookie
}
Function userLogout () // log out
{
Session_start ();
Unset ($ _ SESSION ['username']); // clear username in the session
If (setcookie ('sid ', '', time ()-3600 ))
// Clear the sid in the cookie
Return true;
Else
Return false;
}
Function errReport ($ str) // Error
{
If ($ this-> error_report)
Echo "ERROR: $ str ";
}
}
?> <! -- Php buffer end -->