This article introduces you to the implementation of PHP with the user login and verification of a piece of code, no login and database query separate, there is a need for friends, can refer to learning
Simple login class, not separate login and database query
The code is as follows:
* * * Example * * $Auth =new Auth (), * $Auth->login ("123@123.com", "123"); * $Auth->logout (); * echo $r->init (); ***/
Verify the Login class
The code is as follows:
<?php/* * * @ID: Verify Login Class * * @class: Auth.class.php * * @auther: Hin son * * @time: 2015/03/12 * * @web: http:/ /my.oschina.net/xinger ***/class Auth {//External settings//cookie set var $cookie _time;//7200 var $cookie _where;/ /'/' var $cookie _domain;//' yourweb.com ' var $cookie _secure;//1 and 0//database set Var $selec t_uid;//' uid ' var $select _table;//' user ' var $select _usersname;//' email ' var $select _passwo rd;//' password '//salt var $salt;//"12332" var $guest _name;//' guest '//user get value VA R $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 [' 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; }//Verify 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-& Gt;pre_password]); } else {return false; }}//verify cookie Private Function Check_cookie () {if (!empty ($_cookie[$this->pre_username]) &&!empt Y ($_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". $th Is->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->s ALT); $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 = $v ars[' 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); }//Encrypt 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 ']; }}?>
Summary : The above is the entire content of this article, I hope to be able to help you learn.