Advertisement
Guest User

Untitled

a guest
Aug 12th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.86 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  *
  5.  * @author Robert Main
  6.  * @copyright Robert Main
  7.  * @version 0.0.1
  8.  *
  9.  * This is an authentication class design to help when doing MySQL based
  10.  * authentication.
  11.  *
  12.  *
  13.  * You can modify the properties of this class using the get and set methods
  14.  * provided.
  15.  *
  16.  *
  17.  * If you do not use the get and set methods, the properties default as follows:
  18.  *
  19.  * $hashAltorythm = FALSE (This means plaintext, ie: no password encryption)
  20.  *
  21.  * $tableName = 'users' This is the table where your usernames and passwords
  22.  * are stored
  23.  *
  24.  * $usernameColumn = 'username' This is the column where users usernames are held.
  25.  * If you dont specify a value it defaults to 'username'
  26.  *
  27.  * $passwordColumn = 'password' This is the column where users passwords are held
  28.  * If you dont specify a value it defaults to 'password'
  29.  *
  30.  *
  31.  * $idColumn = 'id' This is the primary key of your users table,
  32.  * if you dont specify this value, the default of 'id' is used.
  33.  *
  34.  *
  35.  *
  36.  */
  37. class Authentication {
  38.  
  39.     private $hashAltorythm = FALSE;
  40.     private $tableName = 'users';
  41.     private $usernameColumn = 'username';
  42.     private $passwordColumn = 'password';
  43.     private $idColumn = 'id';
  44.  
  45.     public function getHashAltorythm() {
  46.         return $this->hashAltorythm;
  47.     }
  48.  
  49.     public function setHashAltorythm($hashAltorythm) {
  50.         $this->hashAltorythm = $hashAltorythm;
  51.     }
  52.  
  53.     public function getTableName() {
  54.         return $this->tableName;
  55.     }
  56.  
  57.     public function setTableName($tableName) {
  58.         $this->tableName = $tableName;
  59.     }
  60.  
  61.     public function getUsernameColumn() {
  62.         return $this->usernameColumn;
  63.     }
  64.  
  65.     public function setUsernameColumn($usernameColumn) {
  66.         $this->usernameColumn = $usernameColumn;
  67.     }
  68.  
  69.     public function getPasswordColumn() {
  70.         return $this->passwordColumn;
  71.     }
  72.  
  73.     public function setPasswordColumn($passwordColumn) {
  74.         $this->passwordColumn = $passwordColumn;
  75.     }
  76.  
  77.     /**
  78.      *
  79.      * @param String $username
  80.      * @param String $password
  81.      */
  82.     public function validate($username, $password) {
  83.         $username = mysql_real_escape_string($username);
  84.         $password = mysql_real_escape_string($password);
  85.         if ($this->hashAltorythm == NULL) {
  86.             $queryResource = mysql_query("SELECT " . $this->usernameColumn . ", " . $this->passwordColumn . " FROM " . $this->tableName . " WHERE (" . $this->usernameColumn . " = '" . $username . "') AND (" . $this->passwordColumn . "= '" . $password . "') ");
  87.         }
  88.         else {
  89.             $queryResource = mysql_query("SELECT " . $this->usernameColumn . ", " . $this->passwordColumn . " FROM " . $this->tableName . " WHERE (" . $this->usernameColumn . " = '" . $username . "') AND (" . $this->passwordColumn . "= '" . hash($this->hashAltorythm, $password) . "') ");
  90.         }
  91.  
  92.         if (mysql_num_rows($queryResource) > 0) {
  93.             return TRUE;
  94.         }
  95.         else {
  96.             return FALSE;
  97.         }
  98.     }
  99.  
  100.     /**
  101.      *
  102.      * @param String $username
  103.      * @return Integer
  104.      */
  105.     private function findUsersID($username) {
  106.         $username = mysql_real_escape_string($username);
  107.         $resource = mysql_query("SELECT " . $this->idColumn . ", " . $this->usernameColumn . ", " . $this->passwordColumn . " FROM " . $this->tableName . " ");
  108.         $user = mysql_fetch_assoc($resource);
  109.  
  110.  
  111.         return $resource[$this->idColumn];
  112.     }
  113.  
  114.     public function loginUser($username) {
  115.         session_start();
  116.         $_SESSION['userid'] = $this->findUsersID($username);
  117.     }
  118.  
  119.     /**
  120.      *
  121.      * @param Integer $userid
  122.      * @return Boolean
  123.      */
  124.     public function isUserLoggedIn($userid) {
  125.         if ($_SESSION['userid'] == $userid) {
  126.             return TRUE;
  127.         }
  128.         else {
  129.             return FALSE;
  130.         }
  131.     }
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement