Guest User

SmalUser

a guest
Nov 29th, 2016
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.61 KB | None | 0 0
  1. <?php
  2. require_once('includes/database.php');
  3.  
  4.  
  5.      function authenticate($username="", $password="", $captcha="") {
  6.         global $database;
  7.         $auth;
  8.         $username = $database->escape_value($username);
  9.         $password = $database->escape_value($password);
  10.  
  11.         if($captcha != ""){
  12.             $cstatus = check_captcha($captcha);
  13.         } else $cstatus = true;
  14.  
  15.         $sql = "SELECT * FROM users ";
  16.         $sql .= "WHERE username = '{$username}' ";
  17.         $sql .= "AND password = '{$password}' ";
  18.         $sql .= "LIMIT 1";
  19.         $result_array = find_by_sql($sql);
  20.         $row = $database->fetch_array($result_array);
  21.  
  22.         //is succesful set sesssion and clear bac login db
  23.         //else increment bad login
  24.  
  25.         if(!empty($row) && $cstatus){
  26.        
  27.          $_SESSION['user_id'] = $row['uID'] ;
  28.           $_SESSION['logged_in'] = true;
  29.           clear_badattempt($username);
  30.           $auth = 1;
  31.         } else{
  32.             if(check_login_attempts($username) >= 3){
  33.                 //update bad attemptd in db ans set $auth
  34.                 update_badattempt($username);
  35.                 $auth =3;
  36.                 if (!$cstatus){
  37.                     $auth = 4;
  38.                 }
  39.             }else{
  40.                 //update bad login attempt in db
  41.                 update_badattempt($username);
  42.                 $auth = 2;
  43.            
  44.             }
  45.         }
  46.             return $auth;
  47.    
  48.     }
  49.  
  50.     function check_captcha($captcha){
  51.         if($captcha == $_SESSION["CaptchaCode"]){
  52.             return true;
  53.         }else return false;
  54.     }
  55.  
  56.  
  57.     function get_data($fname=""){
  58.  
  59.         global $database;
  60.         $fname = $database->escape_value($fname);
  61.  
  62.         $sql = "SELECT * FROM regdata ";
  63.         if($fname != ""){
  64.         $sql .= "WHERE FName like '{$fname}' ";
  65.         }
  66.  
  67.         //run query
  68.         $result_array = find_by_sql($sql);
  69.         return  $database->fetch_all($result_array);
  70.     }
  71.  
  72.  
  73.     // Database Methods
  74.  
  75.  
  76.     function update_badattempt($username=""){
  77.         global $database;
  78.         echo $username;
  79.  
  80.         $sql = "INSERT INTO login_attempt ( ";
  81.         $sql .= "username, ip";
  82.         $sql .= ") VALUES ('";
  83.         $sql .= $username ."', '";
  84.         $sql .= $_SERVER['REMOTE_ADDR'] . "' )";
  85.         if ($database->query($sql)) {
  86.             return true;
  87.         } else {
  88.             return false;
  89.         }
  90.     }
  91.  
  92.        
  93.     function check_login_attempts($username=""){
  94.         global $database;
  95.  
  96.         //time -1hour
  97.         $time = date('Y-m-d H:i:s', time() - 3600);
  98.  
  99.         $username = $database->escape_value($username);
  100.         $sql = "SELECT * FROM login_attempt ";
  101.         $sql .= "WHERE username = '{$username}' and timestemp > '{$time}'";
  102.         $result = $database->query($sql);
  103.         return $database->num_rows($result);
  104.  
  105.     }
  106.  
  107.     function clear_badattempt($username=""){
  108.         global $database;
  109.         $sql = "DELETE FROM login_attempt ";
  110.         $sql .= "WHERE username = '" . $username . "'";
  111.         if ($database->query($sql)) {
  112.             //$this->id = $database->insert_id();
  113.             return true;
  114.         } else {
  115.             return false;
  116.         }
  117.     }
  118.  
  119.  
  120. //return the second lages number in an arrey and find its position.
  121.     $A = array();
  122.     $b = array_slice($A, 1, 1);
  123.  
  124. foreach ($A as  $value) {
  125.     $i++;
  126.     If($value == $b){$c=$i}
  127. }
  128.  
  129.  
  130.      function find_by_sql($sql="") {
  131.         global $database;
  132.         $result_set = $database->query($sql);
  133.         $object_array = array();
  134.         $object_array = $result_set;   
  135.         return $object_array;
  136.     }
  137.  
  138.      function DBinsert($table, $data) {
  139.         global $database;
  140.         //extract arrey keys as columns
  141.         $columns = implode(", ",array_keys($data));
  142.         //excape arrey values
  143.         $escaped_values = $database->escape_array($data);
  144.         //extract values
  145.         $values  = implode("', '", $escaped_values);
  146.         //create sql query
  147.         $sql = "INSERT INTO " . $table . "  ($columns) VALUES ('$values')";
  148.         //run query and return status
  149.         if ($database->query($sql)) {
  150.             return true;
  151.         } else {
  152.             return false;
  153.         }
  154.     }
  155.  
  156. ?>
Add Comment
Please, Sign In to add comment