如何在 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' => '[email protected]', '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 应用程序的任何页面上显示用户信息。这使我们能够为每个用户提供个性化的体验,并使我们的应用程序更易于使用。
广告