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 sessionusername = _SESSION['username'];
?>
<html lang="en">
<head>
<title>How to display logged in user information in PHP?</title>
</head>
<body>
<p>Your username is: <?php echousername; ?></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 sessionusername = _SESSION['username'];
// Simulate retrieving user information from databaseuser_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 echouser_info['name']; ?></p>
<p>Email: <?php echo user_info['email']; ?></p>
<p>Phone: <?php echouser_info['phone']; ?></p>
<p><a href="logout.php">Logout</a></p>
</body>
</html>
结论
在本文中,我们学习了如何在PHP中显示已登录用户的用户信息。通过遵循上述简单的步骤,我们可以轻松地检索和显示用户信息在我们Web应用程序的任何页面上。这使我们能够为每个用户提供个性化的体验,并使我们的应用程序更加用户友好。