Guest User

Untitled

a guest
Feb 20th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.34 KB | None | 0 0
  1. <?php
  2. #Login
  3. if(isset($_POST['doLogin'])) :
  4.    
  5.     $username = mysql_real_escape_string($_POST['username']);
  6.     $password = mysql_real_escape_string($_POST['password']);
  7.        
  8.     // Getting the details
  9.     $grabDetails = "SELECT `username`, `password`, `id`, `isActive` FROM `users` WHERE username = '".$username."' AND password = '".$password."'";
  10.  
  11.     $result = mysql_query($grabDetails) or die(mysql_error());
  12.     $echoResult = mysql_fetch_object($result);
  13.     $countResult = mysql_num_rows($result);
  14.  
  15.     // If found..
  16.     if($countResult == 1 && $echoResult -> isActive == 1) :
  17.            
  18.         $_SESSION['isLogged'] = true;
  19.         $_SESSION['userId'] = $echoResult -> id;
  20.         $_SESSION['username'] = $echoResult -> username;
  21.         $_SESSION['timestamp'] = time();
  22.            
  23.         header("Location: /temp/");
  24.     // If user isn't active
  25.     elseif($countResult == 1 && $echoResult -> isActive != 1):
  26.         header("Location: /temp/index.php?code=1");
  27.     // Default error message
  28.     else:
  29.         header("Location: /temp/index.php?code=2");
  30.     endif;
  31.  
  32. # DB class til sitet
  33.  
  34. <?php
  35.  
  36. class Database{
  37.  
  38.     private $_dbc;
  39.     private $queryError = 'There was an error!';
  40.  
  41.     //* Database connection
  42.     function setRoot ($dbh, $dbu, $dbp, $dbn) {
  43.         $this -> _dbc = mysql_connect($dbh, $dbu, $dbp) or die(mysql_error());
  44.                         mysql_select_db($dbn , $this -> _dbc) or die(mysql_error());
  45.     }
  46.     //* Close connection to 'databaseConnection'.
  47.     function closeDB() {
  48.         mysql_close($this->_dbc);
  49.     }
  50.  
  51.     //* Sanitize input
  52.     function sanitize ($thisInput) {
  53.         return mysql_escape_string(htmlentities(stripslashes($thisInput)));
  54.     }
  55.  
  56.     //* Build a query.
  57.     function buildQuery($table, $type, $fields, $groupKey, $groupResponse, $orderKey, $orderType, $limit) {
  58.         $grouped = '';
  59.         //* If $groupKey is set then
  60.         if($groupKey != ''):
  61.             $grouped = " WHERE `".$groupKey."` = '".$groupResponse."'";
  62.         endif;
  63.         //* If $limit is set then
  64.         $limit = '';
  65.         if($limit != '') :
  66.             $limit = " LIMIT ".$limit;
  67.         endif;
  68.         //* If orderKey is set then:
  69.         $order = '';
  70.         if($orderKey != '') :
  71.             $order = " ORDER BY `".$orderKey ."` ". $orderType."";
  72.         endif;
  73.         //* Manage the $type.
  74.         switch($type) :
  75.             case 'SELECT':
  76.                 $type = "SELECT ".$fields." FROM `".$table."`";
  77.             break;
  78.             case 'DELETE':
  79.                 $type = "DELETE FROM `".$table."`";
  80.             break;
  81.         endswitch;
  82.         //* The return query.
  83.         $returnQuery = "".$type . $grouped . $order . $limit."";
  84.         return $returnQuery;
  85.     }
  86.  
  87.     //* Get all rows from a table with certain criteria.
  88.     function getRows ($table, $groupKey = '', $groupResponse = '', $orderKey = '', $orderType = '', $limit = '', $fields = '*') {
  89.         $buildQuery = $this->buildQuery($table, 'SELECT', $fields, $groupKey, $groupResponse, $orderKey, $orderType, $limit);
  90.         $runQuery = mysql_query($buildQuery) or die($this->queryError . mysql_error());
  91.         while($rows = mysql_fetch_object($runQuery)):
  92.             $returnObjects[] = $rows;
  93.         endwhile;
  94.         return $returnObjects;
  95.     }
  96.  
  97.     //* Get rows from a given ID
  98.     function getRowFromId ($table, $groupKey, $groupResponse, $fields = '*') {
  99.         $thisItem = $this->buildQuery($table, 'SELECT', $fields, $groupKey, $groupResponse);
  100.         $returnItem = mysql_query($thisItem) or die($this->queryError . mysql_error());
  101.         return mysql_fetch_object($returnItem);
  102.     }
  103.  
  104.     //* Count rows from one table.
  105.     function countRows($table, $type, $fields, $groupKey, $groupResponse) {
  106.         $buildQuery = $this->buildQuery($table, $type, $fields, $groupKey, $groupResponse, '','','','');
  107.         $thisReturn = mysql_query($buildQuery);
  108.  
  109.         return mysql_num_rows($thisReturn);
  110.     }
  111.  
  112.     //* Figure out the affected rows.
  113.     function affectedRows($query) {
  114.         return mysql_affected_rows();
  115.     }
  116.  
  117.     //* Insert from an array.
  118.     function insertArray ($table , $insertVals) {
  119.         foreach($insertVals as $key => $val) :
  120.             $keys[] = $key;
  121.             $keyVal[] = '\''.$val.'\'';
  122.         endforeach;
  123.         $keys = implode(', ' , $keys);
  124.         $keyVal = implode(', ', $keyVal);
  125.         $insertQuery = mysql_query("INSERT INTO `".$table."` (".$keys.") VALUES (".$keyVal.")") or die($this->queryError . mysql_error());
  126.         echo 'Added rows: ' . $this->affectedRows($insertQuery);
  127.     }
  128.  
  129.     //* Update from an array.
  130.     function updateArray ($table , $groupKey, $groupResponse, $updateVals) {
  131.         foreach($updateVals as $key => $val) :
  132.             $items[] = $key.'=\''.$val.'\'';
  133.         endforeach;
  134.         $items = implode(', ' , $items);
  135.         $updateQuery = mysql_query("UPDATE `".$table."` SET ".$items." WHERE `".$groupKey."` = '".$groupResponse."'") or die($this->queryError . mysql_error());
  136.         echo $this->affectedRows($updateQuery);
  137.     }
  138.  
  139.     // * Delete from an array.
  140.     function deleteArray($table, $groupKey, $deleteVals){
  141.         foreach($deleteVals as $key) :
  142.             mysql_query("DELETE FROM `".$table."` WHERE `".$groupKey."` = '".$key."'") or die($this->queryError . mysql_error());
  143.         endforeach;
  144.     }
  145. }
Add Comment
Please, Sign In to add comment