PHP Code:
/*
* Name: cnkknd PHP Login Class
* Description: PHP for login class, based on MySQL
* Author: Daniel king,cnkknd@163.com
* Date: 2003/8/25
*/
Class Login
{
var $username; User name
var $userpass; Password
var $userid; User ID
var $userlevel; User Level
var $authtable = "Account"; Validation with Data sheet
var $usecookie =true; Use cookies to save SessionID
var $cookiepath = '/'; Cookie Path
var $cookietime = 108000; Cookie expiry time
var $err _mysql= "MySQL error"; MySQL error message
var $err _username= "username invalid"; Invalid user name prompt
var $err _user= "User invalid"; User Invalid prompt (blocked)
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, connection 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 ()//Determine if login
{
if (isset ($_cookie[' sid '))//If the COOKIE is saved with SID
{
session_id ($_cookie[' Sid ');
Session_Start ();
$this->username=$_session[' username '];
$this->userid=$_session[' userid '];
$this->userlevel=$_session[' userlevel '];
return true;
}
else//If the SID is not 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 blocked
{
$this->errreport ($this->err_user);
$this->err= $this->err_user;
return false;
}
ElseIf (MD5 ($userpass) = = $row [' userpass '])//password matching
{
$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//did not find this user
{
$this->errreport ($this->err_username);
$this->err= $this->err_username;
return false;
}
}
function setsession ()//Place session
{
$sid =uniqid (' Sid '); Generating SIDs
session_id ($SID);
Session_Start ();
$_session[' username ']= $this->username; Assigning a value to a session variable
$_session[' userid ']= $this->userid; //..
$_session[' userlevel ']= $this->userlevel; //..
if ($this->use_cookie)//If you use cookies to save SIDs
{
if (!setcookie (' Sid ', $sid, Time () + $this->cookietime, $this->cookiepath))
$this->errreport ("Set Cookie Failed");
}
Else
Setcookie (' Sid ', ' ', Time ()-3600); Clearing the SID in a cookie
}
function userlogout ()//user logoff
{
Session_Start ();
unset ($_session[' username '); Clear the username in the session
if (Setcookie (' Sid ', ' ', Time ()-3600))
Clearing the SID in a cookie
return true;
Else
return false;
}
function Errreport ($STR)//Error
{
if ($this->error_report)
echo "ERROR: $str";
}
}
?>
Structure of tables in MySQL
Code:
CREATE TABLE ' account ' (
' ID ' bigint (a) 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 ')
)
Use one example
PHP Code:
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. "Have aready logged in";
}
ElseIf ($login->userauth ("danielking", "1234"))
{
echo "Login successfully";
$login->setsession ();
}
echo "
...
";
/*
if ($login->userlogout ())
echo "Logged out";
Else
echo "Logout failed";
*/
?>
The above describes the Photoshop CS5 official Chinese official original download PHP a login class [recommended], including the official Chinese official version of Photoshop CS5 download content, I hope to be interested in PHP tutorial friends helpful.