php登录注册页面完整代码

Creating a complete login and registration system involves both front-end (HTML, CSS) and back-end (PHP, MySQL) components. Below is a simple example of a login and registration system using PHP and MySQL. Please note that this is a basic example, and in a real-world scenario, you should implement additional security measures, such as password hashing and input validation.

index.html (Login Form)

html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Login Page</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="container"> <form action="login.php" method="post"> <h2>Login</h2> <label for="username">Username:</label> <input type="text" name="username" required> <label for="password">Password:</label> <input type="password" name="password" required> <button type="submit">Login</button> </form> <p>Don't have an account? <a href="register.html">Register here</a>.</p> </div> </body> </html>

register.html (Registration Form)

html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Registration Page</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="container"> <form action="register.php" method="post"> <h2>Register</h2> <label for="username">Username:</label> <input type="text" name="username" required> <label for="password">Password:</label> <input type="password" name="password" required> <button type="submit">Register</button> </form> <p>Already have an account? <a href="index.html">Login here</a>.</p> </div> </body> </html>

style.css (Basic Styling)

css
body { font-family: Arial, sans-serif; background-color: #f4f4f4; margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; height: 100vh; } .container { background-color: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); } form { display: flex; flex-direction: column; } label { margin-bottom: 8px; } input { padding: 8px; margin-bottom: 16px; } button { padding: 10px; background-color: #4caf50; color: #fff; border: none; border-radius: 4px; cursor: pointer; } button:hover { background-color: #45a049; } a { color: #007bff; text-decoration: none; } a:hover { text-decoration: underline; }

db.php (Database Connection)

php
<?php $servername = "your_database_host"; $username = "your_database_username"; $password = "your_database_password"; $dbname = "your_database_name"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } ?>

register.php (Registration Script)

php
<?php include('db.php'); if ($_SERVER['REQUEST_METHOD'] == 'POST') { $username = $_POST['username']; $password = password_hash($_POST['password'], PASSWORD_BCRYPT); $sql = "INSERT INTO users (username, password) VALUES ('$username', '$password')"; if ($conn->query($sql) === TRUE) { header('Location: index.html'); } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); } ?>

login.php (Login Script)

php
<?php session_start(); include('db.php'); if ($_SERVER['REQUEST_METHOD'] == 'POST') { $username = $_POST['username']; $password = $_POST['password']; $sql = "SELECT * FROM users WHERE username = '$username'"; $result = $conn->query($sql); if ($result->num_rows > 0) { $row = $result->fetch_assoc(); if (password_verify($password, $row['password'])) { $_SESSION['username'] = $username; header('Location: dashboard.php'); // Redirect to the dashboard page } else { echo "Invalid password"; } } else { echo "Invalid username"; } $conn->close(); } ?>

Make sure to replace placeholder values in the code (like database credentials) with your actual values. Also, create a table named users in your database with columns id, username, and password.

This is a basic example, and in a production environment, you should enhance security, use prepared statements, and consider additional features like password recovery and account verification.

Certainly! Let's continue with the implementation by creating a simple dashboard page (dashboard.php) that users will be redirected to after a successful login. Additionally, I'll provide a logout script (logout.php) to allow users to log out.

dashboard.php (Dashboard Page)

php
<?php session_start(); // Check if the user is logged in if (!isset($_SESSION['username'])) { header('Location: index.html'); exit(); } $username = $_SESSION['username']; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Welcome, <?php echo $username; ?>!</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="container"> <h2>Welcome, <?php echo $username; ?>!</h2> <p>This is your dashboard.</p> <a href="logout.php">Logout</a> </div> </body> </html>

logout.php (Logout Script)

php
<?php session_start(); // Unset all session variables $_SESSION = array(); // Destroy the session session_destroy(); // Redirect to the login page header('Location: index.html'); exit(); ?>

In the dashboard.php file, we check if the user is logged in by verifying the existence of the $_SESSION['username'] variable. If the user is not logged in, they are redirected to the login page (index.html).

The logout.php file unsets all session variables and destroys the session, effectively logging the user out. After that, the user is redirected to the login page.

Ensure that you include session_start() at the beginning of each file that uses session variables.

Remember, this is still a basic example, and in a real-world scenario, you would implement additional security measures and features based on your application requirements.

标签