首頁  >  文章  >  後端開發  >  PHP程式碼:顯示已登入使用者資訊的方法

PHP程式碼:顯示已登入使用者資訊的方法

WBOY
WBOY轉載
2023-09-16 12:33:021382瀏覽

PHP程式碼:顯示已登入使用者資訊的方法

在本文中,我們將學習如何使用 PHP 及其各種內建方法顯示登入的使用者資訊。

在建立需要身份驗證的網路應用程式時,經常需要在各個頁面上顯示登入使用者的資訊。這在電子商務網站、銀行網站等應用程式中可能很有用。我們可以藉助 PHP 及其函數來實現這一點。

讓我們透過一些範例來理解這一點。

範例 1

在此範例中,我們將建立一個登入/登出系統,其中使用者登入後將獲得身份驗證,並將被重定向到儀表板頁面,其資訊可見。然後,使用者可以從儀表板註銷以重置會話。

檔名:login.php

<?php
   session_start();

   if (isset($_POST['username']) && isset($_POST['password'])) {
      $username = $_POST['username'];
      $password = $_POST['password'];

      // Check if username and password are correct (e.g. compare with database)
      // For simplicity, this example only checks if username is 'admin' and password is 'password'
      if ($username === 'admin' && $password === 'password') {
         $_SESSION['username'] = $username;
         header('Location: dashboard.php');
         exit();
      } else {
         $error_message = 'Invalid username or password';
      }
   }
?>

<html lang="en">
<head>
   <title>How to display logged in user information in PHP?</title>
</head>
<body>
      <?php if (isset($error_message)): ?>
         <p><?php echo $error_message; ?></p>
      <?php endif; ?>

   <form method="post">
      <label>
         Username:
         <input type="text" name="username" required>
      </label>

      <br>

      <label>
         Password:
         <input type="password" name="password" required>
      </label>

      <br>

      <button type="submit">Log In</button>
   </form>
</body>
</html>

檔名:logout.php

<?php
   session_start();

  // Unset all of the session variables
   $_SESSION = array();

   // Destroy the session
   session_destroy();

   // Redirect to the login page
   header("Location: login.php");
   exit;
?>

檔名:dashboard.php

#
<?php
   // Start the session
   session_start();

   // Check if user is logged in
   if (!isset($_SESSION['username'])) {
      header("Location: login.php");
      exit;
   }

   // Retrieve user information from session
   $username = $_SESSION['username'];
?>

<html lang="en">
<head>
   <title>How to display logged in user information in PHP?</title>
</head>
<body>
   <p>Your username is: <?php echo $username; ?></p>
   <p><a href="logout.php">Logout</a></p>
</body>
</html>

範例 2

在此範例中,我們將在個人資料頁面上顯示登入使用者的資訊。用戶需要經過身份驗證才能存取個人資料頁面。

檔名:login.php

<?php
   session_start();
   
   if (isset($_POST['username']) && isset($_POST['password'])) {
      $username = $_POST['username'];
      $password = $_POST['password'];

      // Check if username and password are correct (e.g. compare with database)
      // For simplicity, this example only checks if username is 'admin' and password is 'password'
      if ($username === 'admin' && $password === 'password') {
      $_SESSION['username'] = $username;
      header('Location: dashboard.php');
      exit();
      } else {
         $error_message = 'Invalid username or password';
      }
   }
?>

<html lang="en">
<head>
   <title>How to display logged in user information in PHP?</title>
</head>
<body>
   <?php if (isset($error_message)): ?>
      <p><?php echo $error_message; ?></p>
   <?php endif; ?>

   <form method="post">
      <label>
         Username:
         <input type="text" name="username" required>
      </label>

      <br>

      <label>
         Password:
         <input type="password" name="password" required>
      </label>

      <br>

      <button type="submit">Log In</button>
   </form>
</body>
</html>

檔名:logout.php

<?php
   session_start();

   // Unset all of the session variables
   $_SESSION = array();

   // Destroy the session
   session_destroy();

   // Redirect to the login page
   header("Location: login.php");
   exit;
?>

檔名:profile.php

<?php
   // Start the session
   session_start();

   // Check if user is logged in
   if (!isset($_SESSION['username'])) {
      header("Location: login.php");
      exit;
   }

   // Retrieve user information from session
   $username = $_SESSION['username'];

   // Simulate retrieving user information from database
   $user_info = array(
      'name' => 'John Doe',
      'email' => 'john.doe@example.com',
      'phone' => '1234567890',
   );
?>
<html lang="en">
<head>
   <title>Profile Page</title>
</head>
<body>
   <h1>Welcome, <?php echo $username; ?></h1>
   <h2>Profile Information</h2>
   <p>Name: <?php echo $user_info['name']; ?></p>
   <p>Email: <?php echo $user_info['email']; ?></p>
   <p>Phone: <?php echo $user_info['phone']; ?></p>
   <p><a href="logout.php">Logout</a></p>
</body>
</html>

結論

在本文中,我們學習如何在 PHP 中顯示已登入使用者的使用者資訊。透過遵循上述簡單步驟,我們可以輕鬆地在 Web 應用程式的任何頁面上檢索和顯示使用者資訊。這使我們能夠為每個用戶提供個人化的體驗,並使我們的應用程式更加用戶友好。

以上是PHP程式碼:顯示已登入使用者資訊的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除