Advertisement
rasyid03

Untitled

Jun 26th, 2023
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. //admin.php
  2. -------------------------------------------
  3. <?php
  4. // Memeriksa apakah pengguna sudah login atau belum
  5. session_start();
  6. if (!isset($_SESSION['username'])) {
  7. header('Location: /act5/no3/index.php'); // Jika belum login, redirect ke halaman login
  8. exit;
  9. }
  10. ?>
  11.  
  12. <!DOCTYPE html>
  13. <html lang="en">
  14.  
  15. <head>
  16. <meta charset="UTF-8">
  17. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  18. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  19. <title>Faris Rasyid | 50421483</title>
  20. </head>
  21.  
  22. <body>
  23. <h1>Selamat datang, <?php echo $_SESSION['username']; ?></h1>
  24. <p>Ini adalah halaman admin.</p>
  25.  
  26. <a href="logout.php">Logout</a>
  27. </body>
  28.  
  29. </html>
  30.  
  31. ------------------------------------------------------------------
  32. login.php
  33. -------------------------------------------------------------------
  34. <?php
  35. // Simpan informasi login di sini
  36. $valid_username = 'faris';
  37. $valid_password = 'admin';
  38.  
  39. session_start();
  40.  
  41. // Mengecek apakah ada data yang dikirim melalui metode POST
  42. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  43. $username = $_POST['username'];
  44. $password = $_POST['password'];
  45.  
  46. if ($username == $valid_username && $password == $valid_password) {
  47. $_SESSION['username'] = $username;
  48.  
  49. header('Location: /act5/no3/admin.php');
  50. exit;
  51. } else {
  52. $_SESSION['error_message'] = 'Username atau password salah';
  53.  
  54. header('Location: /act5/no3/index.php');
  55. exit;
  56. }
  57. }
  58. ------------------------------------------------------------------
  59. index,php
  60. -----------------------------------------------------------------
  61. <!DOCTYPE html>
  62. <html lang="en">
  63.  
  64. <head>
  65. <meta charset="UTF-8">
  66. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  67. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  68. <title>Faris Rasyid | 50421483</title>
  69. </head>
  70.  
  71. <body>
  72. <h1>Halaman Login</h1>
  73.  
  74. <?php
  75. session_start();
  76. if (isset($_SESSION['error_message'])) {
  77. echo '<p style="color: red;">' . $_SESSION['error_message'] . '</p>';
  78. unset($_SESSION['error_message']); // Menghapus pesan error setelah ditampilkan
  79. }
  80. ?>
  81.  
  82. <form action="login.php" method="POST">
  83. <label for="username">Username:</label>
  84. <input type="text" id="username" name="username" required><br><br>
  85.  
  86. <label for="password">Password:</label>
  87. <input type="password" id="password" name="password" required><br><br>
  88.  
  89. <input type="submit" value="Login">
  90. </form>
  91. </body>
  92.  
  93. </html>
  94. ----------------------------------------------------------------------------
  95. logout.php
  96. ----------------------------------------------------------------------------
  97. <?php
  98. session_start();
  99.  
  100. // Hapus sesi username
  101. unset($_SESSION['username']);
  102.  
  103. // Redirect ke halaman login
  104. header('Location: /act5/no3/index.php');
  105. exit;
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement