写一个通过验证用户名个密码是否正确从而实现两个页面跳转的代码
时间: 2024-03-23 09:37:42 浏览: 173
这是一个简单的通过验证用户名和密码是否正确从而实现两个页面跳转的 PHP 代码示例:
login.html
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form action="check.php" method="post">
<label>Username:</label>
<input type="text" name="username"><br>
<label>Password:</label>
<input type="password" name="password"><br>
<input type="submit" value="Login">
</form>
</body>
</html>
```
check.php
```
<?php
// 假设用户名和密码都是 admin
$username = 'admin';
$password = 'admin';
// 读取表单提交的用户名和密码
$user = $_POST['username'];
$pass = $_POST['password'];
// 验证用户名和密码是否正确
if ($user == $username && $pass == $password) {
// 用户名和密码正确,跳转到 welcome.php 页面
header('Location: welcome.php');
} else {
// 用户名或密码错误,跳转回 login.html 页面
header('Location: login.html');
}
?>
```
welcome.php
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Welcome</title>
</head>
<body>
<h1>Welcome</h1>
<p>Login successful!</p>
</body>
</html>
```
这个示例中,用户在 login.html 页面输入用户名和密码并提交表单,表单数据会被提交到 check.php 程序中。check.php 程序会读取表单提交的用户名和密码,并与预设的用户名和密码进行比较。如果用户名和密码正确,则跳转到 welcome.php 页面;否则跳转回 login.html 页面并提示用户名或密码错误。
阅读全文
相关推荐

















