编写一个登录注册页面,并且对应输入有检查验证的代码
时间: 2023-05-11 20:04:41 浏览: 50
好的,以下是一个简单的登录注册页面的代码,包括输入检查和验证:
HTML代码:
```
<!DOCTYPE html>
<html>
<head>
<title>Login/Register Page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<div class="form-container">
<h2>Login</h2>
<form action="login.php" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<input type="submit" value="Login">
</form>
</div>
<div class="form-container">
<h2>Register</h2>
<form action="register.php" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<label for="confirm_password">Confirm Password:</label>
<input type="password" id="confirm_password" name="confirm_password" required>
<input type="submit" value="Register">
</form>
</div>
</div>
</body>
</html>
```
CSS代码:
```
.container {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
height: 100vh;
}
.form-container {
margin: 20px;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
}
form {
display: flex;
flex-direction: column;
}
label {
margin-bottom: 10px;
}
input[type="text"],
input[type="password"] {
padding: 10px;
margin-bottom: 20px;
border: 1px solid #ccc;
border-radius: 5px;
}
input[type="submit"] {
padding: 10px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #3e8e41;
}
```
PHP代码:
```
<?php
// 验证用户名和密码是否正确
function validate_user($username, $password) {
// 在这里写验证代码
// 如果验证通过,返回 true,否则返回 false
}
// 注册新用户
function register_user($username, $password) {
// 在这里写注册代码
// 如果注册成功,返回 true,否则返回 false
}
// 处理登录请求
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['username']) && isset($_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];
if (validate_user($username, $password)) {
// 登录成功,跳转到主页或其他页面
header('Location: index.php');
exit();
} else {
// 登录失败,显示错误信息
echo 'Invalid username or password.';
}
}
// 处理注册请求
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['username']) && isset($_POST['password']) && isset($_POST['confirm_password'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$confirm_password = $_POST['confirm_password'];
if ($password != $confirm_password) {
// 两次密码不一致,显示错误信息
echo 'Passwords do not match.';
} else {
if (register_user($username, $password)) {
// 注册成功,跳转到登录页面
header('Location: login.php');
exit();
} else {
// 注册失败,显示错误信息
echo 'Registration failed.';
}
}
}
?>
```
以上代码仅供参考,具体实现可能需要根据实际情况进行修改。