Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.50 KB | None | 0 0
  1. <?php
  2. /**
  3.  * This class is used to manage user login/checking
  4.  * It pulls config details from config.txt
  5.  **/
  6. class userManagement{
  7.    
  8.     /**
  9.      * Used to assign the information to the variables
  10.      **/
  11.     function __construct(){
  12.         $this->db_host = parseConfig("db_host");   
  13.         $this->db_user = parseConfig("db_user");
  14.         $this->db_pass = parseConfig("db_pass");
  15.         $this->db_db = parseConfig("db_db");
  16.         $this->db_prefix = parseConfig("db_prefix");
  17.     }
  18.    
  19.     /**
  20.      * This is used to connect to the database, so further
  21.      * operations can be carried out further on in the
  22.      * class.
  23.      **/
  24.     private function mysqlConnect(){
  25.         $this->mysqlCon = mysqli_connect($this->db_host,$this->db_user,$this->db_pass,$this->db_db);
  26.         if(!$this->mysqlCon){
  27.             return "ERROR: Could not connect";
  28.         }
  29.     }
  30.    
  31.     /**
  32.      * Disconnects from the database
  33.      **/
  34.     private function mysqlKill(){
  35.         if($this->mysqlCon){
  36.             mysqli_close($this->mysqlCon);
  37.             return TRUE;
  38.         }else{
  39.             return FALSE;
  40.         }
  41.     }
  42.    
  43.     /**
  44.      * This take 2 params: username and password,
  45.      * it then checks them against the database.
  46.      * This function salts the hash, concatenating
  47.      * username and password, to form a relitavely
  48.      * strong hash.
  49.      **/
  50.     public function userAuth($username,$password){
  51.         $this->mysqlConnect();
  52.         $saltedPass = sha1($username.$password);
  53.         $mysqlData = mysqli_query($this->mysqlCon,"SELECT username, password, uid FROM ".$this->db_prefix."users WHERE username = '".$username."' AND password = '".$saltedPass."'");
  54.         if(!$mysqlData){
  55.             session_unset();
  56.             $_SESSION["userAuthed"] = FALSE;
  57.             return FALSE;
  58.         }
  59.        
  60.         if(mysqli_num_rows($mysqlData) == 1){
  61.             $usersTable = mysqli_fetch_array($mysqlData);
  62.             $_SESSION["userAuthed"] = TRUE;
  63.             $_SESSION["username"] = $username;
  64.             $_SESSION["uid"] = $row["uid"];
  65.             return TRUE;
  66.         }
  67.         $this->mysqlKill();
  68.     }
  69.    
  70.     /**
  71.      * The following function is used when a user
  72.      * registers on the site, and inputs their
  73.      * data into the users, and userinfo tables.
  74.      **/
  75.      public function userRegister($username, $password, $email, $name){
  76.          $this->mysqlConnect();
  77.          $saltedPass = sha1($username.$password);
  78.          if(!mysqli_query($this->mysqlCon,"INSERT INTO ".$this->db_prefix."users (username,password,email,name) VALUES ($username,$password,$email,$name)")){
  79.              return FALSE;
  80.          }else{
  81.              $message = "Thank you $name for registering to the ".parseConfig("title")." $name. I hope this works.";
  82.              mail($email,"Thank you for registering!",$message);
  83.              return TRUE;
  84.          }
  85.      }
  86. }
  87. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement