如何在 PHP 中顯示已登入使用者資訊?
在本文中,我們將學習如何使用 PHP 及其各種內建方法來顯示已登入使用者資訊。
在構建需要身份驗證的 Web 應用程式時,通常需要在各個頁面上顯示已登入使用者的使用者資訊。這在電子商務網站、銀行網站等應用程式中可能很有用。我們可以藉助 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 應用程式的任何頁面上顯示使用者資訊。這使我們能夠為每個使用者提供個性化的體驗,並使我們的應用程式更易於使用。
廣告