A php login class [recommended] _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags set cookie
A php login class [recommended]. PHP code :? ** Name: cnkndphploginclass * Description: PHP for login class, based on MySQL * author: DanielKing, cnkknd@163.com * Date: 2003825 * classLogin {var $ username; with PHP code:
/*
* Name: CnkknD PHP Login Class
* Description: PHP logon class. it is based on MySQL.
* By 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"; // 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 ";
}
}
?>

Table structure in mysql


Code:
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 ')
)


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. "has aready logged in ";
}
Elseif ($ login-> userAuth ("danielking", "1234 "))
{
Echo "login successfully ";
$ Login-> setSession ();
}

Echo"

...

";

/*
If ($ login-> userLogout ())
Echo "logged out ";
Else
Echo "logout failed ";
*/
?>


Why :? /** Name: cnkndphploginclass * Description: PHP for login class, based on MySQL * author: DanielKing, cnkknd@163.com * Date: 2003/8/25 */classLogin {var $ username; // use...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.