Advertisement
Guest User

Untitled

a guest
Nov 26th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. <?php
  2. include("config/config.php");
  3. if (isset($_POST['login'])) {
  4.  
  5. $username = filter_input(INPUT_POST, 'username',FILTER_SANITIZE_STRING);
  6. $password = filter_input(INPUT_POST, 'password',FILTER_SANITIZE_STRING);
  7.  
  8. $sentence = "";
  9.  
  10. if (empty($username) || empty($password)) {
  11. $sentence .= "username dan password tidak boleh kosong";
  12. }
  13.  
  14. $query = "SELECT * FROM users WHERE username =:username";
  15.  
  16. $query_param = array(
  17. ":username" => $username
  18. );
  19.  
  20. try {
  21. $stmt = $db->prepare($query);
  22. $result = $stmt->execute($query_param);
  23.  
  24. } catch (PDOException $error) {
  25. die("error cant access");
  26. }
  27. $login_ok = false;
  28. $row = $stmt->fetch();
  29.  
  30. // jika garapan database sama dengan post cek pssword
  31. if ($row) {
  32. // mencoba hash gabungan post password & salt
  33. $check_password = hash('sha256', $_POST['password'] . $row['salt']);
  34. // login true
  35. if ($check_password === $row['password']) {
  36. $login_ok = true;
  37. }
  38. }
  39. if ($login_ok) {
  40. // menyembunyikan row salt dan password
  41. unset($row['salt']);
  42. unset($row['password']);
  43.  
  44. // memasukan semua garapan $row ke session
  45. $_SESSION['user'] = $row['username'];
  46. header("Location: index.php");
  47. }else{
  48. $sentence .= "Login failed";
  49. exit();
  50. }
  51.  
  52. }
  53. else{
  54. $sentence ="";
  55. $username ="";
  56. $password = "";
  57. }
  58.  
  59. ?>
  60. <!DOCTYPE html>
  61. <html>
  62. <head>
  63. <title>Login Form</title>
  64. <link rel="stylesheet" type="text/css" href="assets/form.css">
  65. </head>
  66. <body>
  67. <div class="container">
  68. <h1>Selamat Datang</h1>
  69. <h3>Sistem Informasi sekolahku</h3>
  70. <?php
  71. // tampilkan pesan jika ada
  72. if (isset($pesan)) {
  73. echo "<div class=\"pesan\">$pesan</div>";
  74. }
  75.  
  76. // tampilkan error jika ada
  77. if ($sentence !== "") {
  78. echo "<div class=\"error\">$sentence</div>";
  79. }
  80. ?>
  81. <form action="login.php" method="post">
  82. <fieldset>
  83. <legend>Login</legend>
  84. <p>
  85. <label for="username">Username : </label>
  86. <input type="text" name="username" id="username"
  87. value="<?php echo $username ?>">
  88. </p>
  89. <p>
  90. <label for="password">Password : </label>
  91. <input type="password" name="password" id="password"
  92. value="<?php echo $username ?>">
  93. </p>
  94. <button class="button" type="submit" name="login">Login</button>
  95. </fieldset>
  96. <p style="text-align: center;"><a href="register.php">not have account? register now</p>
  97.  
  98. </form>
  99. </div>
  100. </body>
  101. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement