Advertisement
Guest User

Untitled

a guest
Apr 10th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. <?php
  2. session_start();
  3. require 'functions.php';
  4. //cek cookie
  5. if(isset($_COOKIE['id']) && isset($_COOKIE['key']) ){
  6. $id = $_COOKIE['id'];
  7. $key = $_COOKIE['key'];
  8. //ambil username berdasarkan id
  9. $result= mysqli_query($conn, "SELECT username FROM user WHERE id='$id'");
  10. $row = mysqli_fetch_assoc($result);
  11.  
  12. //cek cookie dan username
  13. if ($key === hash('sha256', $row['username'])) {
  14. $_SESSION['login'] = true;
  15. }
  16.  
  17. }
  18.  
  19.  
  20. //cek tombol masuk sudah ditekan atau belum
  21. if ( isset($_POST["masuk"])) {
  22. $username = $_POST["username"];
  23. $password = $_POST["password"];
  24.  
  25. $result = mysqli_query($conn, "SELECT * FROM user WHERE
  26. username='$username'");
  27. //cek username
  28. if (mysqli_num_rows($result) === 1) {
  29. //cek password
  30. $row = mysqli_fetch_assoc($result);
  31. if (password_verify($password, $row['password']))
  32. //set session
  33. $_SESSION["login"]= true;
  34.  
  35. // header("Location: index.php");
  36. // exit;
  37. // }
  38.  
  39.  
  40. }
  41.  
  42. //cek remember me
  43. if (isset($_POST["remember"])) {
  44. //buat cookie
  45.  
  46. setcookie('id', $row['id'], time()+60);
  47. setcookie('key', hash('sha256',$row['username']), time()+60);
  48. if ($_POST["username"] == "admin" && $_POST["password"] == "admin") {
  49. header("Location: indexadmin.php");
  50. exit;
  51. } else {
  52. header("Location: index.php");
  53. exit;
  54. }
  55. }
  56. }
  57.  
  58. if (isset($_SESSION["login"])) {
  59. if ($_POST["username"] == "admin" && $_POST["password"] == "admin") {
  60. header("Location: indexadmin.php");
  61. exit;
  62. } else {
  63. header("Location: index.php");
  64. exit;
  65. }
  66. }
  67.  
  68. if (isset($_POST["daftar"])) {
  69. header("Location: registrasi.php");
  70. exit;
  71.  
  72. }
  73.  
  74.  
  75. ?>
  76.  
  77. <!DOCTYPE html>
  78. <html>
  79. <head>
  80. <meta charset="utf-8">
  81. <title>Halaman Login</title>
  82. <link rel="stylesheet" href="../css/login.css">
  83. </head>
  84. <body>
  85. <h2>LibrarIH</h2>
  86. <h1>selamat datang <br>di perpustakaan <br>itikurih hibarna</h1>
  87. <?php if (isset($error)): ?>
  88. <p style="color:red; font-style:italic">Username/password salah!</p>
  89. <?php endif; ?>
  90. <form class="form" action="" method="post">
  91.  
  92. <input type="text" name="username" id="username" placeholder="nama pengguna">
  93. <input type="password" name="password" id="password" placeholder="kata sandi"><br>
  94. <td>
  95. <input type="checkbox" name="remember" id="remember">
  96. <label class="remember"for="remember">Ingat Saya</label></td><br>
  97. <button "button"type="submit" name="masuk">masuk</button> <br>
  98. <label for="daftar">belum punya akun?</label> <br>
  99. <a href="../nyoba/registrasi.php"><button type="submit" name="daftar"> daftar</button></a>
  100.  
  101.  
  102.  
  103. </form>
  104. </body>
  105. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement