Simple login class that does not separate login and database queries
Copy Code code as follows:
/*
* Examples
*
* $Auth =new Auth ();
* $Auth->login ("123@123.com", "123");
* $Auth->logout ();
* Echo $r->init ();
*
**/
Verify Landing class
Copy Code code as follows:
<?php
/*
*
* @ID: Verify the Landing class
*
* @class: Auth.class.php
*
* @auther: Hin son
*
* @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 '
User gets value
var $user _id;
var $username;
var $ok;
var $pre;//' Auth_ '
var $depr//'-'
Internal variables
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 [' Depr '])? $config [' Depr ']: '-';
}
//
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;
}
Validate 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;
}
}
Verifying cookies
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;
}
}
Landing
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-& Gt;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 <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, 0,8) ==substr (MD5 (substr ($result, 8). $key), 0,8))
{
Return substr ($result, 8);
}
Else
{
Return ";
}
}
Else
{
return str_replace (' = ', ', ' Base64_encode ($result));
}
}
Public Function get_ip () {
Return $_server[' REMOTE_ADDR '];
}
}
?>
The above is the entire content of this article, I hope you can enjoy.