Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.07 KB | None | 0 0
  1. app
  2.   -lib
  3.      -config.php
  4.      -database.php
  5.      -auth.php
  6.      -user.php
  7.   -index.php
  8.  
  9.  
  10. ------------------------------------------------------------------------------------------------
  11.  
  12. database.php
  13.  
  14. <?php
  15.  
  16. require_once('config.php');
  17.  
  18. class MySQLDatabase{
  19.  
  20.     private $connection;
  21.    
  22.     public function __construct(){
  23.    
  24.         $this->connect_database();
  25.     }
  26.    
  27.     public function connect_database(){
  28.        
  29.         $this->connection = mysql_connect("localhost", "root", "password");
  30.         if (!$this->connection){
  31.        
  32.             die("Cannot connect to database " . mysql_error());
  33.        
  34.         } else {
  35.        
  36.             $select_db = mysql_select_db('portal');
  37.            
  38.             if(!$select_db){
  39.            
  40.                 die("Error in selecting database " . mysql_error());
  41.             }
  42.         }
  43.     }
  44.    
  45.     public function query($sql){
  46.    
  47.         return mysql_query($sql, $this->connection);
  48.    
  49.     }
  50.    
  51.     public function fetch_array($result_set){
  52.    
  53.         return mysql_fetch_array($result_set);
  54.    
  55.     }
  56.    
  57.     public function num_rows($result_set){
  58.    
  59.         return mysql_num_rows($result_set);
  60.    
  61.     }
  62.  
  63.     public function close_connection(){
  64.    
  65.         if(isset($this->connection)) {
  66.             mysql_close($this->connection);
  67.             unset($this->connection);
  68.         }
  69.    
  70.     }
  71.  
  72. }
  73.  
  74. $database = new MySQLDatabase();
  75.  
  76. ---------------------------------------------------------------------------------------------
  77. user.php
  78.  
  79. <?php
  80.  
  81. require_once('database.php');
  82.  
  83. class User {
  84.  
  85.     public function insert($tablename, $username, $password, $email_address){
  86.    
  87.         $result = $this->query("INSERT INTO $tablename(username, password, email_address) VALUES('$username','$password','$email_address')");
  88.         if($result){
  89.            
  90.             return TRUE;
  91.        
  92.         } else {
  93.        
  94.             return FALSE;
  95.        
  96.         }
  97.            
  98.     }
  99.    
  100.  
  101.  
  102. }
  103.  
  104. ----------------------------------------------------------------------------------------------
  105.  
  106. auth.php
  107.  
  108.  
  109. <?php
  110. require_once('database.php');
  111.  
  112.  
  113. class Auth extends MySQLDatabase{
  114.  
  115.     public static function authenticate_user($username, $password){
  116.         global $database;
  117.         $sql = "SELECT username, password FROM users ";
  118.         $sql .= "WHERE username ='" . $username ."' ";
  119.         $sql .= "AND password ='" . $password ."'";
  120.        
  121.  
  122.         $result_set = $database->query($sql);
  123.         if(!$result_set){
  124.            
  125.             die(mysql_error());
  126.        
  127.         }
  128.         if($database->num_rows($result_set) == 1){
  129.        
  130.             return TRUE;
  131.        
  132.         }
  133.        
  134.         return FALSE;
  135.     }
  136.    
  137.  
  138. }
  139.  
  140. $auth = new Auth();
  141.  
  142.  
  143.  
  144. ---------------------------------------------------------------------------------------------
  145.  
  146. index.php
  147.  
  148. <?php
  149. require_once('lib/config.php');
  150. require_once('lib/database.php');
  151. require_once('lib/auth.php');
  152. require_once('lib/user.php');
  153.  
  154. if($_POST['submit']){
  155.  
  156.     $check_auth = $auth->authenticate_user($_POST['username'] ,$_POST['password']);
  157.    
  158.     if($check_auth){
  159.        
  160.         echo "welcome";
  161.    
  162.    
  163.     }
  164.  
  165. }
  166. ?>
  167. <form method="post" action="index.php" >
  168. <fieldset>
  169. <legend accesskey="l">Login</legend>
  170.  
  171. <label for="username">Username :</label>
  172. <input type="text" name="username" />
  173. <label for="password">Password :</label>
  174. <input type="password" name="password" />
  175. <input type="submit" value="login" name="submit" />
  176. </fieldset>
  177.  
  178. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement