Advertisement
rasyid03

act5/no3

May 29th, 2023 (edited)
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.47 KB | Source Code | 0 0
  1. ----------------------------------------------------//No2 index.html-------------------------------------
  2. <!DOCTYPE html>
  3. <html lang="en">
  4.  
  5. <head>
  6.   <meta charset="UTF-8">
  7.   <meta http-equiv="X-UA-Compatible" content="IE=edge">
  8.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  9.   <title>Faris Rasyid | 50421483</title>
  10. </head>
  11.  
  12. <body>
  13.   <h1>Cari Lokasi Universitas</h1>
  14.   <form action="hasil.php" method="GET">
  15.     <fieldset>
  16.       <label for="university">Masukkan nama universitas:</label>
  17.       <input type="text" id="university" name="university" required><br><br>
  18.     </fieldset>
  19.  
  20.     <input type="submit" value="Cari Lokasi">
  21.   </form>
  22. </body>
  23.  
  24. </html>
  25.  
  26. -----------------------------------------//No2 hasil.php--------------------------------------
  27.  
  28. <?php
  29.  
  30. echo "<h1>Lokasi Universitas</h1>";
  31.  
  32. if ($_SERVER["REQUEST_METHOD"] == "GET") {
  33.   $university = $_GET['university'];
  34.  
  35.   $lokasi = "";
  36.  
  37.   if ($university == "gundar" || $university == "universitas gunadarma") {
  38.     $lokasi = "Depok";
  39.   } elseif ($university == "ui" || $university == "universitas indonesia") {
  40.     $lokasi = "Depok";
  41.   } elseif ($university == "gunadarma" || $university == "universitas indonesia") {
  42.     $lokasi = "Depok";
  43.   } elseif ($university == "itb" || $university == "institut teknologi bandung") {
  44.     $lokasi = "Bandung";
  45.   } elseif ($university == "undip" || $university == "universitas diponogoro") {
  46.     $lokasi = "Semarang";
  47.   } elseif ($university == "ugm" || $university == "universitas gajah mada") {
  48.     $lokasi = "Yogyakarta";
  49.   } else {
  50.     $lokasi = "tidak diketahui";
  51.   }
  52.  
  53.   if ($lokasi == "Tidak diketahui") {
  54.     echo "<p>Lokasi dari $university $lokasi</p>";
  55.   } else {
  56.     echo "<p>$university berada di $lokasi</p>";
  57.   }
  58. }
  59.  
  60. ---------------------------------------//admin.php-----------------------------------
  61.  
  62. <?php
  63. // Memeriksa apakah pengguna sudah login atau belum
  64. session_start();
  65. if (!isset($_SESSION['username'])) {
  66.   header('Location: /act5/no3/index.php'); // Jika belum login, redirect ke halaman login
  67.   exit;
  68. }
  69. ?>
  70.  
  71. <!DOCTYPE html>
  72. <html lang="en">
  73.  
  74. <head>
  75.   <meta charset="UTF-8">
  76.   <meta http-equiv="X-UA-Compatible" content="IE=edge">
  77.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  78.   <title>Faris Rasyid | 50421483</title>
  79. </head>
  80.  
  81. <body>
  82.   <h1>Selamat datang, <?php echo $_SESSION['username']; ?></h1>
  83.   <p>Ini adalah halaman admin.</p>
  84.  
  85.   <a href="/act5/no3/logout.php">Logout</a>
  86. </body>
  87.  
  88. </html>
  89.  
  90. ---------------------------------------------//index.php--------------------------------
  91.  
  92. <!DOCTYPE html>
  93. <html lang="en">
  94.  
  95. <head>
  96.   <meta charset="UTF-8">
  97.   <meta http-equiv="X-UA-Compatible" content="IE=edge">
  98.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  99.   <title>Faris Rasyid | 50421483</title>
  100. </head>
  101.  
  102. <body>
  103.   <h1>Halaman Login</h1>
  104.  
  105.   <?php
  106.   session_start();
  107.   if (isset($_SESSION['error_message'])) {
  108.     echo '<p style="color: red;">' . $_SESSION['error_message'] . '</p>';
  109.     unset($_SESSION['error_message']); // Menghapus pesan error setelah ditampilkan
  110.   }
  111.   ?>
  112.  
  113.   <form action="login.php" method="POST">
  114.     <label for="username">Username:</label>
  115.     <input type="text" id="username" name="username" required><br><br>
  116.  
  117.     <label for="password">Password:</label>
  118.     <input type="password" id="password" name="password" required><br><br>
  119.  
  120.     <input type="submit" value="Login">
  121.   </form>
  122. </body>
  123.  
  124. </html>
  125.  
  126. ------------------------------------//Login.php-------------------------------
  127.  
  128. <?php
  129. // Simpan informasi login di sini
  130. $valid_username = 'faris';
  131. $valid_password = 'admin';
  132.  
  133. session_start();
  134.  
  135. // Mengecek apakah ada data yang dikirim melalui metode POST
  136. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  137.   $username = $_POST['username'];
  138.   $password = $_POST['password'];
  139.  
  140.   if ($username == $valid_username && $password == $valid_password) {
  141.     $_SESSION['username'] = $username;
  142.  
  143.     header('Location: /act5/no3/admin.php');
  144.     exit;
  145.   } else {
  146.     $_SESSION['error_message'] = 'Username atau password salah';
  147.  
  148.     header('Location: /act5/no3/index.php');
  149.     exit;
  150.   }
  151. }
  152.  
  153. -----------------------------------//logout.php----------------------------------
  154.  
  155. <?php
  156. session_start();
  157.  
  158. // Hapus sesi username
  159. unset($_SESSION['username']);
  160.  
  161. // Redirect ke halaman login
  162. header('Location: /act5/no3/index.php');
  163. exit;
  164.  
  165. -----------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement