PHP login verification sharing

Source: Internet
Author: User
This article introduces a piece of code for user login and verification implemented in php. the login and database query are not separated. if you need a friend, you can refer to the simple login class for learning, login and database query are not separated

The code is as follows:


/*
* Example
*
* $ Auth = new Auth ();
* $ Auth-> login ("123@123.com", "123 ");
* $ Auth-> logout ();
* Echo $ r-> init ();
*
**/

Verify login class

The code is as follows:


<? Php
/*
*
* @ ID: authentication login class
*
* @ Class: Auth. class. php
*
* @ Auther: Hiner
*
* @ Time: 2015/03/12
*
* @ Web: http://my.oschina.net/xinger
*
**/
Class Auth {
// External settings
// Cookie settings
Var $ cookie_time; // 7200
Var $ cookie_where ;//'/'
Var $ cookie_domain; // 'yourweb. com'
Var $ cookie_secure; // 1 and 0
// Database settings
Var $ select_uid; // 'uid'
Var $ select_table; // 'user'
Var $ select_usersname; // 'email'
Var $ select_password; // 'password'
// Salt
Var $ salt; // 12332"
Var $ guest_name; // 'guest'
// The user obtains the value.
Var $ user_id;
Var $ username;
Var $ OK;
Var $ pre; // 'auth _'
Var $ depr ;//'-'
// Internal variable
Private $ pre_username;
Private $ pre_password;
Public function _ construct ($ config = array ()){
$ This-> set ($ config );
$ This-> pre_username = sha1 (md5 ($ this-> pre. 'Username '));
$ This-> pre_password = sha1 (md5 ($ this-> pre. 'password '));
}
Public function set ($ config ){
$ This-> cookie_time = isset ($ config ['cookie _ time'])? $ Config ['cookie _ time']: 7200;
$ This-> cookie_where = isset ($ config ['cookie _ where'])? $ Config ['cookie _ where']: '/';
$ This-> cookie_domain = isset ($ config ['cookie _ domain '])? $ Config ['cookie _ domain ']: '';
$ This-> cookie_secure = isset ($ config ['cookie _ secure '])? $ Config ['cookie _ secure ']: '';
$ This-> select_uid = isset ($ config ['Select _ uid'])? $ Config ['Select _ uid']: 'uid ';
$ This-> select_table = isset ($ config ['Select _ table'])? $ Config ['Select _ table']: 'table ';
$ This-> select_usersname = isset ($ config ['Select _ usersname'])? $ Config ['Select _ usersname']: 'user _ name ';
$ This-> select_password = isset ($ config ['Select _ password'])? $ Config ['Select _ password']: 'password ';
$ This-> salt = isset ($ config ['Salt'])? $ Config ['Salt']: 'sghsdghsdg ';//
$ This-> guest_name = isset ($ config ['Guest _ name'])? $ Config ['Guest _ name']: 'guest ';//
$ This-> pre = isset ($ config ['auth'])? $ Config ['auth']: 'auth _';
$ This-> depr = isset ($ config ['dep'])? $ Config ['dep']: '-';
}
//
Public function init (){
$ This-> user_id = 0;
$ This-> username = $ this-> guest_name;
$ This-> OK = false;
If (! $ This-> check_session ()){
$ This-> check_cookie ();
}
Return $ this-> OK;
}
// Verify the SESSION
Private function check_session (){
If (! Empty ($ _ SESSION [$ this-> pre_username]) &! Empty ($ _ SESSION [$ this-> pre_password]) {
Return $ this-> check ($ _ SESSION [$ this-> pre_username], $ _ SESSION [$ this-> pre_password]);
} Else {
Return false;
}
}
// Verify the COOKIE
Private function check_cookie (){
If (! Empty ($ _ COOKIE [$ this-> pre_username]) &! Empty ($ _ COOKIE [$ this-> pre_password]) {
Return $ this-> check ($ _ COOKIE [$ this-> pre_username], $ _ COOKIE [$ this-> pre_password]);
} Else {
Return false;
}
}
// Login
Public function login ($ username, $ password ){
$ SQL = "select ". $ this-> select_uid. "from ". $ this-> select_table. "where ". $ this-> select_usersname. "= '$ username' and ". $ this-> select_password. "= '$ password '";
$ Result = mysql_query ($ SQL );
$ Rows = mysql_num_rows ($ SQL );
If ($ rows = 1 ){
$ This-> user_id = mysql_result ($ result, 0, 0 );
$ This-> username = $ username;
$ This-> OK = true;
$ Username = $ username. $ this-> depr. $ this-> get_ip ();
$ User_name = $ this-> encrypt ($ username, 'e', $ this-> salt );
$ _ SESSION [$ this-> pre_username] = $ user_name;
$ _ SESSION [$ this-> pre_password] = md5 (md5 ($ password, $ this-> salt ));
Setcookie ($ this-> pre_username, $ user_name, time () + $ this-> cookie_time, $ this-> cookie_where, $ this-> cookie_domain, $ this-> cookie_secure );
Setcookie ($ this-> pre_password, md5 (md5 ($ password, $ this-> salt), time () + $ this-> cookie_time, $ this-> cookie_where, $ this-> cookie_domain, $ this-> cookie_secure );
Return true;
}
Return false;
}
// Verify
Private function check ($ username, $ password ){
$ User_name = $ this-> encrypt ($ username, 'D', $ this-> salt );
$ Name = explode ($ this-> depr, $ user_name );
$ Username = $ name [0];
$ Ip = isset ($ name [1])? $ Name [1]: NULL;
If ($ ip! ==$ This-> get_ip () return false;
Static $ vars = array ();
If (! Empty ($ vars) & is_array ($ vars) & isset ($ vars [$ username. $ password]) {
$ This-> user_id = $ vars ['User _ id'];
$ This-> username = $ vars ['username'];
$ This-> OK = $ vars ['OK'];
Return true;
}
$ SQL = "select ". $ this-> select_uid. ",". $ this-> select_password. "from ". $ this-> select_table. "where ". $ this-> select_usersname. "= '$ username '";
$ Query = mysql_query ($ SQL );
$ Result = mysql_fetch_array ($ query );
$ Row = mysql_num_rows ($ SQL );
If ($ row = 1 ){
$ Db_password = $ result [$ this-> select_password];
If (md5 (md5 ($ db_password, $ this-> salt) ==$ password ){
$ This-> user_id = $ vars ['User _ id'] = $ result [$ this-> select_uid];
$ This-> username = $ vars ['username'] = $ username;
$ This-> OK = $ vars ['OK'] = true;
$ Vars [$ username. $ password] = md5 ($ username. $ password );
Return true;
}
}
Return false;
}
// Exit
Public function logout (){
$ This-> user_id = 0;
$ This-> username = $ this-> guest_name;
$ This-> OK = false;
$ _ SESSION [$ this-> pre_username] = "";
$ _ SESSION [$ this-> pre_password] = "";
Setcookie ($ this-> pre_username, "", time ()-$ this-> cookie_time, $ this-> cookie_where, $ this-> cookie_domain, $ this-> cookie_secure );
Setcookie ($ this-> pre_password, "", time ()-$ this-> cookie_time, $ this-> cookie_where, $ this-> cookie_domain, $ this-> cookie_secure );
}
// Encryption
Public function encrypt ($ string, $ operation, $ key = ''){
$ Key = md5 ($ key );
$ Key_length = strlen ($ key );
$ String = $ operation = 'D '? Base64_decode ($ string): substr (md5 ($ string. $ key), 0, 8). $ string;
$ String_length = strlen ($ string );
$ Rndkey = $ box = array ();
$ Result = '';
For ($ I = 0; $ I <= 255; $ I ++)
{
$ Rndkey [$ I] = ord ($ key [$ I % $ key_length]);
$ Box [$ I] = $ I;
}
For ($ j = $ I = 0; I I <256; $ I ++)
{
$ J = ($ j + $ box [$ I] + $ rndkey [$ I]) % 256;
$ Tmp = $ box [$ I];
$ Box [$ I] = $ box [$ j];
$ Box [$ j] = $ tmp;
}
For ($ a = $ j = $ I = 0; $ I <$ string_length; $ I ++)
{
$ A = ($ a + 1) % 256;
$ J = ($ j + $ box [$ a]) % 256;
$ Tmp = $ box [$ a];
$ Box [$ a] = $ box [$ j];
$ Box [$ j] = $ tmp;
$ Result. = chr (ord ($ string [$ I]) ^ ($ box [($ box [$ a] + $ box [$ j]) % 256]);
}
If ($ operation = 'd ')
{
If (substr ($ result,) = substr (md5 (substr ($ result, 8). $ key ))
{
Return substr ($ result, 8 );
}
Else
{
Return '';
}
}
Else
{
Return str_replace ('=', '', base64_encode ($ result ));
}
}
Public function get_ip (){
Return $ _ SERVER ['remote _ ADDR '];
}
}
?>

The above is all the content of this article. I hope you will like it.

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.