Advertisement
Guest User

Untitled

a guest
Aug 31st, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. <?php
  2. function redirect_to($new_location){
  3. header("location: " . $new_location);
  4. exit;
  5. }
  6.  
  7. function ensure($test){
  8. if (!$test){
  9. die ("Database querying has failed");
  10. }
  11. }
  12.  
  13. function password_encrypt($password){
  14. $hash_format="$2y$10$"; //Telling php to use blowfish of 10 character by $2y and $10$
  15. $salt_length=22; //Salting by blowfish
  16. $salt=generate_salt($salt_length);//Will be find out in a function
  17. $format_and_salt=$hash_format . $salt; //Performing the salt
  18. $hash = crypt($password, $format_and_salt); //Sallting The password
  19. return $hash;
  20. }
  21.  
  22. function generate_salt($salt_length){
  23.  
  24. $unique_random_string=md5(uniqid(mt_rand(), true));
  25. $base64_encode=base64_encode($unique_random_string);
  26. $modified_base64_string=str_replace('+', '.', $base64_encode);
  27.  
  28.  
  29. $salt = substr($modified_base64_string, 0, $salt_length);
  30. return $salt;
  31.  
  32. }
  33.  
  34.  
  35. function find_by_username($username){
  36. global $connection;
  37. $safe = mysqli_real_escape_string($connection, $username);
  38. $query= "SELECT * FROM admin ";
  39. $query.= "WHERE username='{$safe}' ";
  40. $query.= " LIMIT 1";
  41. $admin_set=mysqli_query($connection, $query);
  42. ensure($admin_set);
  43. if($admin = mysqli_fetch_assoc($admin_set)){
  44. return $admin;
  45. }
  46. }
  47.  
  48. function find_by_id($admin_id){
  49. global $connection;
  50. $safe = mysqli_real_escape_string($connection, $admin_id);
  51. $query= "SELECT * FROM admin ";
  52. $query.= "WHERE id='{$safe}' ";
  53. $query.= " LIMIT 1";
  54. $admin_set=mysqli_query($connection, $query);
  55. ensure($admin_set);
  56. if($admin = mysqli_fetch_assoc($admin_set)){
  57. return $admin;
  58. }
  59. }
  60.  
  61.  
  62. function password_check($password, $existing_hash){
  63.  
  64. $hash=crypt($password, $existing_hash);
  65. if($hash === $existing_hash){
  66. return true;
  67. }else{
  68. return false;
  69. }
  70.  
  71. }
  72.  
  73. function attempt_login($username, $password){
  74.  
  75. $admin=find_by_username($username);
  76. if($admin){
  77. if(password_check($password, $admin["password"])){
  78. return $admin;
  79. }else{
  80. return false;
  81. }
  82. }else{
  83. return false;
  84. }
  85.  
  86. }
  87.  
  88. <?php session_start()?>
  89. <?php require_once("DB_conn.php");?>
  90. <?php require_once("function.php");?>
  91. <html lang="en">
  92. <head>
  93. <title>Titan Store</title>
  94.  
  95. <link href="public.css" media="all" rel="stylesheet" type="text/css" />
  96. </head>
  97. <body>
  98. <?php
  99. $Slected=current_id();
  100. ?>
  101. <?php
  102. $subject_result=getting_all_subject();
  103. ?>
  104. <div id ="header">
  105. <h1>Titan Store</h1>
  106. </div>
  107. <div id ="main">
  108. <div id="navigation">
  109. <ul class="titan">
  110. <?php
  111. while($row=mysqli_fetch_assoc($subject_result)){
  112.  
  113. ?>
  114. <li class="selected">
  115. <a href="About.php?sub=<?php echo $row["id"]; ?>">
  116. <?php echo $row["position"]; echo "."; echo $row["menu_name"]; ?></a>
  117.  
  118. </li>
  119.  
  120. <ul class="pages">
  121. <?php
  122. $page_result=getting_all_pages($row["id"]);
  123. ?>
  124. <?php
  125. while ($row_page=mysqli_fetch_assoc($page_result)){
  126.  
  127. ?>
  128.  
  129. <li>
  130. <a href="Home.php?page=<?php echo $row_page["id"]?>">
  131. <?php echo $row_page["menu_name"]; ?></a>
  132. </li>
  133. <?php
  134. }
  135. ?>
  136.  
  137. <?php mysqli_free_result($page_result)?>
  138. </ul>
  139.  
  140. <?php
  141. }
  142. ?>
  143. <?php mysqli_free_result($subject_result);?>
  144. </ul>
  145. </div>
  146. <div id= "pages">
  147. <br/>
  148.  
  149. <a href= "Admin.php"> See All Users Lists>></a>
  150.  
  151.  
  152. <?php
  153. if(isset($_SESSION["message"])){
  154. echo"<div class="message">";
  155. echo $_SESSION ["message"];
  156. echo"</div>";
  157.  
  158.  
  159. $_SESSION["message"]= null;
  160. }
  161. ?>
  162. <?php
  163. if(isset($_POST['login'])){
  164. $username=$_POST["username"];
  165. $password=$_POST["password"];
  166. $found_admin=attempt_login($username, $password);
  167. if($found_admin){
  168. $_SESSION["admin_id"]=$found_admin["id"];
  169. $_SESSION["username"]=$found_admin["username"];
  170. redirect_to("manage_content.php");
  171. }else{
  172. $_SESSION["message"]="Username/Password was wrong.";
  173. }
  174. }
  175. ?>
  176. <h1>Log In</h1>
  177. <br/>
  178. <?php
  179. if(isset($_SESSION["message"])){
  180. echo"<div class="message">";
  181. echo $_SESSION ["message"];
  182. echo"</div>";
  183.  
  184. $_SESSION["message"]= null;
  185. }
  186. ?>
  187. <h2>Please Login</h2>
  188. <ul>
  189. <form action = "admins.php" method = "post">
  190. <p> Username : <input type="text" name="username" value=""></p>
  191. <p> Password : <input type="password" name="password" value=""></p><br/>
  192. <p><input type="submit" name="login" value="Log In"><p>
  193. </form>
  194. </div>
  195. </ul>
  196. </div>
  197. <?php require_once("Footer.php");?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement