PHP MySql Login


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. Class Auth{
  3. private $mysql_db;
  4. private $mysql_user;
  5. private $mysql_pass;
  6. private $mysql_server;
  7. private $mysql_port;
  8.  
  9. private $table_name;
  10. private $table_nickCol;
  11. private $table_passCol;
  12. private $table_loginAttemps;
  13.  
  14. private $db_connection;
  15. private $query;
  16.  
  17. private $userName;
  18. private $userPass;
  19.  
  20. public function setDb( $server, $user, $pass, $db, $port = 3306 ){
  21. $this -> mysql_db = $db;
  22. $this -> mysql_user = $user;
  23. $this -> mysql_pass = $pass;
  24. $this -> mysql_server = $server;
  25. $this -> mysql_port = $port;
  26. }
  27.  
  28. public function setTable( $name, $nickCol, $passCol, $loginAttemps ){
  29. $this -> table_name = $name;
  30. $this -> table_nickCol = $nickCol;
  31. $this -> table_passCol = $passCol;
  32. $this -> table_loginAttemps = $loginAttemps;
  33.  
  34. }
  35.  
  36. public function dbConnect(){
  37. if( !$this -> checkServer($this -> mysql_server) ){
  38. throw new Exception( 'Server is DOWN.' );
  39. }
  40.  
  41. if( !($this ->db_connection = @mysql_connect($this -> mysql_server . ':' . $this -> mysql_port, $this -> mysql_user, $this -> mysql_pass)) ){
  42. throw new Exception( 'Can\'t connect to MySql Server. <br /><b>' . mysql_error() . '</b>' );
  43. }
  44.  
  45. if( !@mysql_selectdb( $this ->mysql_db, $this -> db_connection ) ){
  46. throw new Exception( 'Can\'t connect to data base.<br /><b>' . mysql_error() . '<br />' );
  47. }
  48. }
  49.  
  50. public function logIn( $userName, $userPass ){
  51. if( !$this -> existTable( $this -> table_name )){
  52. throw new Exception( 'MySql error.<br /><b>Table <i>' . $this -> table_name . '</i> couldn\'t be found in data base</b>' );
  53. }
  54.  
  55. if( !$this -> existField($this -> table_nickCol) || !$this -> existField($this -> table_passCol) ){
  56. throw new Exception( 'MySql error.<br /><b>Couldn\'t find the necessary fields in table <i>' . $this -> table_name . '</i></b>' );
  57. }
  58.  
  59. $this -> userName = $userName;
  60. $this -> userPass = $userPass;
  61.  
  62. if( !$this -> confirmUser() ){
  63. throw new Exception( 'Can\'t do <b>Login</b>.<br />Username o password incorrect.' );
  64. }
  65.  
  66. if( !$this -> confirmLogin() ){
  67. $this -> increaseAttempts();
  68. throw new Exception( 'Can\'t do <b>Login</b>.<br />Username o password incorrect.' );
  69. }
  70. }
  71.  
  72. private function confirmUser(){
  73. $sql = sprintf('SELECT ' . $this -> table_nickCol .
  74. ' FROM ' . $this -> table_name .
  75. ' WHERE LOWER(' . $this -> table_nickCol . ') = \'%s\' LIMIT 1;',
  76. strtolower($this -> stringEscape($this -> userName)) );
  77.  
  78. $query = mysql_query( $sql, $this -> db_connection );
  79. $confirm = ( mysql_num_rows($query) > 0 )? true : false;
  80. mysql_free_result( $query );
  81. return( $confirm );
  82. }
  83.  
  84. private function increaseAttempts(){
  85. $sql = sprintf('UPDATE ' . $this -> table_name .
  86. ' SET ' . $this -> table_loginAttemps . ' = ' . $this -> table_loginAttemps . ' + 1
  87. WHERE LOWER(' . $this -> table_nickCol . ') = \'%s\';',
  88. strtolower($this -> stringEscape($this -> userName)) );
  89.  
  90. mysql_query( $sql, $this -> db_connection );
  91. }
  92.  
  93. private function confirmLogin(){
  94. $sql = sprintf( 'SELECT * FROM ' . $this -> table_name .
  95. ' WHERE LOWER(' .$this -> table_nickCol . ') = \'%s\'
  96. AND ' . $this -> table_passCol . ' = md5(\'%s\') LIMIT 1;',
  97. strtolower($this -> stringEscape($this -> userName)),
  98. $this -> stringEscape($this -> userPass) );
  99.  
  100. $query = mysql_query( $sql, $this -> db_connection );
  101. $confirm= ( mysql_num_rows($query) > 0 )? true : false;
  102. if( $confirm ) $this -> makeSessions( mysql_fetch_assoc($query) );
  103. mysql_free_result( $query );
  104. return( $confirm );
  105. }
  106.  
  107. private function makeSessions( $data ){
  108. print $data['userNick'];
  109. }
  110.  
  111. private function stringEscape( $string ){
  112. $string = stripslashes( $string );
  113. }
  114. $string = mysql_real_escape_string($string, $this -> db_connection);
  115. return( $string );
  116. }
  117.  
  118. private function checkServer( $server ){
  119. $file = @fsockopen($server, $this -> mysql_port, $errno, $errstr, 10 );
  120. $status = false;
  121. if( $file ){
  122. fclose($file);
  123. $status = true;
  124. }
  125. return $status;
  126. }
  127.  
  128. private function existTable( $table ){
  129. $exist = false;
  130. $querry = mysql_query( 'SHOW tables FROM ' . $this -> mysql_db . ' LIKE ' . '\'' . $table . '\'', $this -> db_connection );
  131. if( mysql_num_rows($querry) == 1 ){
  132. $exist = true;
  133. }
  134. return( $exist );
  135. }
  136.  
  137. private function existField( $field ){
  138. $exist = false;
  139. $query = mysql_query( 'SHOW COLUMNS FROM ' .$this -> table_name . ' LIKE ' . '\'' . $field . '\'', $this -> db_connection );
  140. if( mysql_num_rows($query) == 1 ){
  141. $exist = true;
  142. }
  143. return( $exist );
  144. }
  145.  
  146. public function closeDb(){
  147. $ok = true;
  148. if( !@mysql_close( $this -> db_connection ) ){
  149. $ok = false;
  150. }
  151. return( $ok );
  152. }
  153. }
  154.  
  155. $login = new Auth();
  156. $login -> setDb( '127.0.0.1', 'root', '', 'web');
  157. $login -> setTable( 'users', 'userNick', 'userPass', 'loginAttemps' );
  158. try{
  159. $login -> dbConnect();
  160. $login -> logIn( 'test', 'test' );
  161. $login -> closeDb();
  162.  
  163. }catch( Exception $e ){
  164. print $e -> getMessage();
  165. }
  166. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.