用php写一个用户注册系统
时间: 2023-12-31 07:06:32 浏览: 150
以下是一个简单的用户注册系统的代码示例:
1. 创建数据库表
```sql
CREATE TABLE users (
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(255) NOT NULL,
email VARCHAR(100) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
```
2. 创建注册页面(register.php)
```php
<!DOCTYPE html>
<html>
<head>
<title>User Registration</title>
</head>
<body>
<h2>Register</h2>
<?php if (!empty($errors)) { ?>
<div style="color:red;">
<?php foreach ($errors as $error) {
echo $error . "<br>";
} ?>
</div>
<?php } ?>
<form method="post" action="register.php">
<label>Username:</label>
<input type="text" name="username" value="<?php echo $username; ?>"><br><br>
<label>Password:</label>
<input type="password" name="password"><br><br>
<label>Email:</label>
<input type="email" name="email" value="<?php echo $email; ?>"><br><br>
<input type="submit" name="submit" value="Register">
</form>
</body>
</html>
```
3. 处理注册数据(register.php)
```php
<?php
$errors = [];
$username = '';
$email = '';
if (isset($_POST['submit'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
// 验证表单数据
if (empty($username)) {
$errors[] = 'Username is required';
}
if (empty($password)) {
$errors[] = 'Password is required';
}
if (empty($email)) {
$errors[] = 'Email is required';
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = 'Email is invalid';
}
// 如果没有错误,则插入数据到数据库
if (empty($errors)) {
$conn = mysqli_connect('localhost', 'username', 'password', 'dbname');
$username = mysqli_real_escape_string($conn, $username);
$password = password_hash($password, PASSWORD_DEFAULT);
$email = mysqli_real_escape_string($conn, $email);
$sql = "INSERT INTO users (username, password, email) VALUES ('$username', '$password', '$email')";
if (mysqli_query($conn, $sql)) {
header('Location: login.php');
exit;
} else {
$errors[] = 'Registration failed';
}
}
}
// 显示注册页面
include 'register.php';
?>
```
4. 创建登录页面(login.php)
```php
<!DOCTYPE html>
<html>
<head>
<title>User Login</title>
</head>
<body>
<h2>Login</h2>
<?php if (!empty($errors)) { ?>
<div style="color:red;">
<?php foreach ($errors as $error) {
echo $error . "<br>";
} ?>
</div>
<?php } ?>
<form method="post" action="login.php">
<label>Username:</label>
<input type="text" name="username"><br><br>
<label>Password:</label>
<input type="password" name="password"><br><br>
<input type="submit" name="submit" value="Login">
</form>
</body>
</html>
```
5. 处理登录数据(login.php)
```php
<?php
$errors = [];
if (isset($_POST['submit'])) {
$username = $_POST['username'];
$password = $_POST['password'];
// 验证表单数据
if (empty($username)) {
$errors[] = 'Username is required';
}
if (empty($password)) {
$errors[] = 'Password is required';
}
// 如果没有错误,则验证用户名和密码是否正确
if (empty($errors)) {
$conn = mysqli_connect('localhost', 'username', 'password', 'dbname');
$username = mysqli_real_escape_string($conn, $username);
$sql = "SELECT * FROM users WHERE username='$username'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
$user = mysqli_fetch_assoc($result);
if (password_verify($password, $user['password'])) {
session_start();
$_SESSION['user_id'] = $user['id'];
header('Location: profile.php');
exit;
} else {
$errors[] = 'Password is incorrect';
}
} else {
$errors[] = 'Username not found';
}
}
}
// 显示登录页面
include 'login.php';
?>
```
6. 创建用户资料页面(profile.php)
```php
<?php
session_start();
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
$user_id = $_SESSION['user_id'];
$conn = mysqli_connect('localhost', 'username', 'password', 'dbname');
$sql = "SELECT * FROM users WHERE id='$user_id'";
$result = mysqli_query($conn, $sql);
$user = mysqli_fetch_assoc($result);
?>
<!DOCTYPE html>
<html>
<head>
<title>User Profile</title>
</head>
<body>
<h2>User Profile</h2>
<p><strong>Username:</strong> <?php echo $user['username']; ?></p>
<p><strong>Email:</strong> <?php echo $user['email']; ?></p>
<p><a href="logout.php">Logout</a></p>
</body>
</html>
```
7. 创建注销页面(logout.php)
```php
<?php
session_start();
session_destroy();
header('Location: login.php');
exit;
?>
```
这就是一个简单的用户注册系统的实现,当然还可以根据需求进行改进和优化。
阅读全文