A logged in class

Source: Internet
Author: User
?
/*
* 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"; Validating data tables

var $usecookie =true; Save SessionID with Cookies
var $cookiepath = '/'; Cookie Path
var $cookietime = 108000; Cookie valid time

var $err _mysql= "MySQL error"; MySQL Error prompt
var $err _username= "username invalid"; Invalid user name prompt
var $err _user= "User invalid"; Invalid user prompt (banned)
var $err _password= "Password error"; Password error hint

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 ()//To determine whether to log in
{
if (isset ($_cookie[' sid '))//If there is a SID stored in the COOKIE
{
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 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//did not find this user
{
$this->errreport ($this->err_username);
$this->err= $this->err_username;
return false;
}
}

function setsession ()//session
{
$sid =uniqid (' Sid '); Build SID
session_id ($SID);
Session_Start ();
$_session[' username ']= $this->username; Assigning values to session variables
$_session[' userid ']= $this->userid; //..
$_session[' userlevel ']= $this->userlevel; //..
if ($this->use_cookie)//If the SID is saved with a cookie
{
if (!setcookie (' Sid ', $sid, Time () + $this->cookietime, $this->cookiepath))
$this->errreport ("Set Cookie Failed");
}
Else
Setcookie (' Sid ', ', ', Time ()-3600); To clear the SID in a cookie
}

function userlogout ()//user logoff
{
Session_Start ();
unset ($_session[' username ')); Clear the username in session
if (Setcookie (' Sid ', ', ', Time ()-3600))
To clear 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 not NULL auto_increment,
' username ' varchar (255) Not NULL
Use a case


PHP Code:
?
Include ".. /myclasses/login.php ";
$dbserv = "localhost";
$dbport = "3306";
$dbuser = "root";
$dbpass = "123456";
$dbname = "Test";

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.