Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.06 KB | None | 0 0
  1. <?php
  2. global $db;
  3. // require("../config/db.php");
  4. global $error1, $error2, $error3, $error4;
  5. $full_name = $username = $password = "";
  6.  
  7.  
  8. if(isset($_POST['submit'])){
  9. $username = $_POST['username'];
  10. $ad_password = $_POST['password'];
  11. $full_name = $_POST['full_name'];
  12.  
  13. $sql_query = mysqli_query($db, "SELECT username FROM admin WHERE username = '{$username}' ");
  14. $count = mysqli_num_rows($sql_query);
  15.  
  16. $sql_salt = mysqli_query($db, "SELECT randSaltPass FROM admin");
  17. $row = mysqli_fetch_array($sql_salt);
  18. $salt = $row['randSaltPass'];
  19. $password = crypt($ad_password, $salt);
  20.  
  21. if(!empty($username) && !empty($ad_password) && !empty($full_name)){
  22.  
  23. if($count > 0){
  24. $error1 = "<div class='alert alert-danger'>
  25. <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
  26. Username Already Exists.
  27. </div>";
  28. }else{
  29.  
  30. $u_name = mysqli_real_escape_string($db, $username);
  31. $pass_word = mysqli_real_escape_string($db, $ad_password);
  32. $admin_name = mysqli_real_escape_string($db, $full_name);
  33.  
  34. if(!preg_match('/^[a-zA-Z]*$/', $u_name)){
  35.  
  36. $error2 ="<div class='alert alert-danger'>
  37. <a href='' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
  38. Only Leters are Allowed For Username.
  39. </div>";
  40. }
  41. if(!preg_match('/^[a-zA-Z]*$/', $admin_name)){
  42. $error3 ="<div class='alert alert-danger'>
  43. <a href='' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
  44. Only Leters are Allowed For Fullname.
  45. </div>";
  46. }
  47.  
  48. if(!preg_match('/^S*(?=S{7,15})(?=S*[a-z])(?=S*[A-Z])(?=S*[d])S*$/', $pass_word)){
  49. $error4 ="<div class='alert alert-danger'>
  50. <a href='' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
  51. Password Must Be Between 7 and 15 Characters and Must Contain At Least One Lowercase Letter one uppercase Letter and One Digit.
  52. </div>";
  53. }
  54.  
  55.  
  56. if((preg_match('/^[a-zA-Z]*$/', $u_name)) && (preg_match('/^[a-zA-Z]*$/', $admin_name)) && (preg_match('/^S*(?=S{7,15})(?=S*[a-z])(?=S*[A-Z])(?=S*[d])S*$/', $pass_word))){
  57.  
  58.  
  59. $sql = "INSERT INTO admin(username, password, admin_name) VALUES('{$u_name}', '{$password}', '{$admin_name}' )";
  60.  
  61. $query = mysqli_query($db, $sql);
  62.  
  63. if(!$query){
  64. die("QUERY FAILED " . mysqli_error($db));
  65. }
  66.  
  67. }
  68.  
  69.  
  70. }
  71.  
  72. }else{
  73.  
  74. if(empty($username)){
  75. $error2="<div class='alert alert-danger'>
  76. <a href='' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
  77. Username Can Be Empty.
  78. </div>";
  79. }
  80. if(empty($full_name)){
  81. $error3="<div class='alert alert-danger'>
  82. <a href='' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
  83. Fullname Can Be Empty.
  84. </div>";
  85. }
  86. if(empty($password)){
  87. $error4="<div class='alert alert-danger'>
  88. <a href='' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
  89. Password Can Be Empty.
  90. </div>";
  91. }
  92.  
  93. }
  94.  
  95. }
  96.  
  97.  
  98.  
  99. ?>
  100.  
  101. <?php
  102.  
  103.  
  104. class Admin_Login
  105. {
  106. private $_username;
  107. private $_password;
  108.  
  109. public function __construct($c_username, $c_password) {
  110. $this->_username = $c_username;
  111. $this->_password = md5($c_password);
  112.  
  113. // $sql_salt = mysqli_query($db, "SELECT randSaltPass FROM admin");
  114. // $row = mysqli_fetch_array($sql_salt);
  115. // $salt = $row['randSaltPass'];
  116. // $password = crypt($db, $salt);
  117. }
  118.  
  119. public function AdminLogin() {
  120. global $db;
  121.  
  122. //Start session
  123. session_start();
  124.  
  125. //Array to validate errors
  126. $error_msg_array = array();
  127.  
  128. //Error messages
  129. $error_msg = FALSE;
  130.  
  131. if($this->_username == "") {
  132. $error_msg_array[] = "Please input your username";
  133. $error_msg = TRUE;
  134. }
  135.  
  136. if($this->_password == "") {
  137. $error_msg_array[] = "Please input your password";
  138. $error_msg = TRUE;
  139. }
  140.  
  141. if($error_msg) {
  142. $_SESSION['ERROR_MSG_ARR'] = $error_msg_array;
  143. header("location: http://localhost/voting_system/sandbox/index.php");
  144. exit();
  145. }
  146.  
  147. $sql = "SELECT * FROM admin WHERE username = ? AND password = ? LIMIT 1";
  148. if(!$stmt = $db->prepare($sql)) {
  149. echo $stmt->error;
  150. } else {
  151. $stmt->bind_param("ss", $this->_username, $this->_password);
  152. $stmt->execute();
  153. $result = $stmt->get_result();
  154. }
  155.  
  156. if($result->num_rows > 0) {
  157. //Login successful
  158. $row = $result->fetch_assoc();
  159.  
  160. //Create session
  161. session_regenerate_id();
  162. $_SESSION['ADMIN_ID'] = $row["id"];
  163. $_SESSION['ADMIN_NAME'] = $row["name"];
  164. session_write_close();
  165.  
  166. header("location: http://localhost/voting_system/sandbox/admin_page.php");
  167.  
  168. } else {
  169. //Login failed
  170. $error_msg_array[] = "The username and password you entered is incorrect.";
  171. $error_msg = TRUE;
  172.  
  173. if($error_msg) {
  174. $_SESSION['ERROR_MSG_ARR'] = $error_msg_array;
  175. header("location: http://localhost/voting_system/sandbox/index.php");
  176. exit();
  177. }
  178. $stmt->free_result();
  179. }
  180. $result->free();
  181. return $result;
  182. }
  183. }
  184.  
  185. <?php
  186. //Include database connection
  187. require("../../config/db.php");
  188.  
  189. //Include class Admin_Login
  190. require("../classes/Admin_Login.php");
  191.  
  192. if(isset($_POST['submit'])) {
  193.  
  194. //Create variable to store post array values
  195. $username = trim($_POST['username']);
  196. $password = trim($_POST['password']);
  197.  
  198. $adminLogin = new Admin_Login($username, $password);
  199. $rtnAdminLogin = $adminLogin->AdminLogin();
  200.  
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement