PHP 登录界面模板是一个用于用户登录的网页界面,通常包含用户名和密码输入框,以及一个提交按钮。用户输入正确的用户名和密码后,系统会验证这些信息,并根据验证结果决定是否允许用户访问受保护的资源。
以下是一个简单的 PHP 登录界面模板示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.login-container {
border: 1px solid #ccc;
padding: 20px;
width: 300px;
}
.login-container h2 {
margin-bottom: 20px;
text-align: center;
}
.form-group {
margin-bottom: 15px;
}
.form-group label {
display: block;
margin-bottom: 5px;
}
.form-group input {
width: 100%;
padding: 8px;
box-sizing: border-box;
}
.form-group button {
width: 100%;
padding: 10px;
background-color: #007BFF;
color: white;
border: none;
cursor: pointer;
}
.form-group button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<div class="login-container">
<h2>Login</h2>
<form action="login.php" method="post">
<div class="form-group">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
</div>
<div class="form-group">
<button type="submit">Login</button>
</div>
</form>
</div>
</body>
</html><?php
session_start();
// 假设这是从数据库获取的正确用户名和密码
$correct_username = 'admin';
$correct_password = 'password';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = $_POST['username'];
$password = $_POST['password'];
if ($username === $correct_username && $password === $correct_password) {
$_SESSION['loggedin'] = true;
$_SESSION['username'] = $username;
header('Location: dashboard.php');
exit();
} else {
$error = 'Invalid username or password.';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<div class="login-container">
<h2>Login</h2>
<?php if (isset($error)) { ?>
<p style="color: red;"><?php echo $error; ?></p>
<?php } ?>
<form action="login.php" method="post">
<div class="form-group">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
</div>
<div class="form-group">
<button type="submit">Login</button>
</div>
</form>
</div>
</body>
</html>通过以上示例代码,你可以创建一个基本的 PHP 登录界面,并实现简单的用户验证功能。根据实际需求,你可以进一步扩展和优化这个模板。
没有搜到相关的文章