<?php
session_start();
// Dummy user database (replace with actual database in production)
$users = [
['username' => 'user1', 'password' => 'password1'],
['username' => 'user2', 'password' => 'password2']
];
// Function to check if a username exists in the database
function isUsernameExists($username) {
global $users;
foreach ($users as $user) {
if ($user['username'] === $username) {
return true;
}
}
return false;
}
// Function to validate user credentials (for login)
function isValidUser($username, $password) {
global $users;
foreach ($users as $user) {
if ($user['username'] === $username && $user['password'] === $password) {
return true;
}
}
return false;
}
// Function to register a new user
function registerUser($username, $password) {
global $users;
// Normally would hash the password, but keeping it simple for this example
$users[] = ['username' => $username, 'password' => $password];
}
// Login logic
if (isset($_POST['login'])) {
$username = $_POST['username'];
$password = $_POST['password'];
if (isValidUser($username, $password)) {
$_SESSION['username'] = $username;
header('Location: dashboard.php'); // Redirect to dashboard or homepage
exit;
} else {
$loginError = "Invalid username or password";
}
}
// Registration logic
if (isset($_POST['register'])) {
$username = $_POST['username'];
$password = $_POST['password'];
if (!isUsernameExists($username)) {
registerUser($username, $password);
$registrationSuccess = "Registration successful. You can now login.";
} else {
$registrationError = "Username already exists. Please choose another.";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Registration and Login System</title>
</head>
<body>
<h1>User Registration and Login System</h1>
<!-- Registration form -->
<h2>Register</h2>
<?php if(isset($registrationError)) { echo "<p>$registrationError</p>"; } ?>
<?php if(isset($registrationSuccess)) { echo "<p>$registrationSuccess</p>"; } ?>
<form action="" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br>
<button type="submit" name="register">Register</button>
</form>
<!-- Login form -->
<h2>Login</h2>
<?php if(isset($loginError)) { echo "<p>$loginError</p>"; } ?>
<form action="" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br>
<button type="submit" name="login">Login</button>
</form>
</body>
</html>
这个PHP应用包含以下几个关键功能:
-
注册功能: 用户可以填写用户名和密码进行注册。系统会检查用户名是否已存在,若不存在则允许注册,否则会显示用户名已存在的错误信息。
-
登录功能: 已注册用户可以使用填写的用户名和密码进行登录。系统会验证用户输入的凭据是否正确,若正确则会创建一个会话,并重定向到用户的仪表板或主页。
-
会话管理: 使用
session_start()
函数开启会话,通过$_SESSION
变量来存储和检索当前用户的会话信息。 -
错误处理: 对于无效的登录尝试或重复的用户名注册,系统会显示相应的错误消息,帮助用户了解问题所在。
这个简单的用户注册与登录系统展示了如何使用PHP处理用户输入、进行基本的数据验证和会话管理。这种类型的系统适合于小型网站或应用程序,可以作为学习PHP基础、原型开发或个人项目的起点。