写一个基于html,css,php的拥有登录和注册功能的页面的代码,且登陆成功后可以跳转到index.html
时间: 2023-11-27 15:53:22 浏览: 120
以下是一个简单的基于HTML、CSS和PHP的登录和注册页面示例,当用户成功登录后,将跳转到index.html页面。
HTML代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>Login and Register</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="login-box">
<div class="login-header">
<h2>Login Here</h2>
</div>
<form action="login.php" method="post">
<div class="form-group">
<label>Username</label>
<input type="text" name="username" required>
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="password" required>
</div>
<button type="submit" class="btn" name="login_user">Login</button>
</form>
<div class="register">
<p>Don't have an account? <a href="register.php">Sign up</a></p>
</div>
</div>
</body>
</html>
```
CSS代码:
```css
body {
margin: 0;
padding: 0;
background-color: #e8eff5;
}
.login-box {
width: 400px;
height: 400px;
background-color: #fff;
margin: 100px auto;
padding: 20px;
border-radius: 10px;
box-shadow: 0px 0px 15px 5px rgba(0,0,0,0.1);
}
.login-header h2 {
font-size: 28px;
color: #333;
text-align: center;
margin-top: 0;
}
.form-group {
margin-bottom: 20px;
}
.form-group label {
display: block;
font-size: 18px;
color: #333;
margin-bottom: 5px;
}
.form-group input {
width: 100%;
height: 40px;
border: 1px solid #ccc;
border-radius: 5px;
padding: 5px 10px;
font-size: 16px;
color: #333;
}
.btn {
display: block;
width: 100%;
height: 40px;
background-color: #4CAF50;
color: #fff;
font-size: 18px;
border: none;
border-radius: 5px;
margin-top: 20px;
cursor: pointer;
}
.btn:hover {
background-color: #3e8e41;
}
.register {
text-align: center;
margin-top: 20px;
}
.register p {
font-size: 16px;
color: #333;
}
.register a {
color: #4CAF50;
}
```
PHP代码,login.php:
```php
<?php
session_start();
// initializing variables
$username = "";
$errors = array();
// connect to the database
$db = mysqli_connect('localhost', 'root', '', 'test');
// LOGIN USER
if (isset($_POST['login_user'])) {
$username = mysqli_real_escape_string($db, $_POST['username']);
$password = mysqli_real_escape_string($db, $_POST['password']);
if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}
if (count($errors) == 0) {
$password = md5($password);
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$results = mysqli_query($db, $query);
if (mysqli_num_rows($results) == 1) {
$_SESSION['username'] = $username;
header('location: index.html');
}else {
array_push($errors, "Wrong username/password combination");
}
}
}
?>
```
PHP代码,register.php:
```php
<?php
session_start();
// initializing variables
$username = "";
$email = "";
$errors = array();
// connect to the database
$db = mysqli_connect('localhost', 'root', '', 'test');
// REGISTER USER
if (isset($_POST['reg_user'])) {
// receive all input values from the form
$username = mysqli_real_escape_string($db, $_POST['username']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
$password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
// form validation: ensure that the form is correctly filled
if (empty($username)) { array_push($errors, "Username is required"); }
if (empty($email)) { array_push($errors, "Email is required"); }
if (empty($password_1)) { array_push($errors, "Password is required"); }
if ($password_1 != $password_2) {
array_push($errors, "The two passwords do not match");
}
// register user if there are no errors in the form
if (count($errors) == 0) {
$password = md5($password_1);//encrypt the password before saving in the database
$query = "INSERT INTO users (username, email, password)
VALUES('$username', '$email', '$password')";
mysqli_query($db, $query);
$_SESSION['username'] = $username;
header('location: index.html');
}
}
?>
```
请注意,这只是一个简单的示例,不应用于生产环境,并且需要根据您的需求进行更改和改进。
阅读全文