Home >Backend Development >PHP Problem >How to log in to the message board in php
With the development of the Internet, message boards are one of the important components of the website. It provides an interactive platform for users to leave messages, communicate and share opinions. In the message board, users can log in and leave their own information. For website developers, it is necessary to implement a simple and easy-to-use login message board. This article will introduce how to use PHP language to log in to the message board.
1. Environment preparation
Before starting development, you need to install a PHP integrated development environment, such as XAMPP, WAMP, MAMP, etc. Here we take XAMPP as an example. After installation, we need to place the project files in the htdocs directory of XAMPP. After that, open the browser and go to http://localhost/phpmyadmin/, create a database named "message_board", and create a table named "messages" in it, which should contain the following fields: id, name, email, message and timestamp. Among them, the id field is the auto-increasing primary key, and the timestamp field is the current time.
2. Page design
In PHP, we need to design a series of interfaces and interactive processes to log in to the message board. Our first goal is to create a login page where the user enters their username and password to authenticate and jump to the message board page.
The following is a simple login page source code:
<!DOCTYPE html> <html> <head> <title>Login Page</title> </head> <body> <h2>Login Here</h2> <form action="login.php" method="post"> <label>Username:</label> <input type="text" name="username"><br><br> <label>Password:</label> <input type="password" name="password"><br><br> <input type="submit" value="Login"> </form> </body> </html>
When the user clicks the "Login" button, the form will be submitted and then jump to the login.php page. On this page, we need to verify from the database whether the user name and password entered by the user are correct. If the verification is passed, set the session variables and then jump to the message board page. If verification fails, the login page is returned with an error message.
The following is the source code of the login.php page:
<?php /*检查登录信息并跳转*/ session_start(); $username = $_POST['username']; $password = $_POST['password']; /*数据库连接*/ $servername = "localhost"; $db_username = "root"; $db_password = ""; /*默认密码为空*/ $dbname = "message_board"; $conn = new mysqli($servername, $db_username, $db_password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } /*获取用户信息*/ $sql = "SELECT * FROM users WHERE username='$username' AND password='$password'"; $result = $conn->query($sql); if ($result->num_rows > 0) { /*验证通过,创建会话变量*/ $_SESSION['username'] = $username; header("Location: message_board.php"); } else { /*验证失败,返回登陆页面*/ echo "Invalid username/password. Please try again."; header("Location: login.html"); } $conn->close(); ?>
3. Message board implementation
After completing the login page and login verification, we need to design and implement the message board page . This page needs to display all messages and provide a form for users to leave messages. Like the login page, when the user submits the message form, we need to get all the messages from the database and display them on the page.
The following is the source code of the message board page:
<?php /*检查会话变量并跳转*/ session_start(); if (!isset($_SESSION['username'])) { header("Location: login.html"); } /*数据库连接*/ $servername = "localhost"; $db_username = "root"; $db_password = ""; $dbname = "message_board"; $conn = new mysqli($servername, $db_username, $db_password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } /*获取所有留言*/ $sql = "SELECT * FROM messages"; $result = $conn->query($sql); ?> <!DOCTYPE html> <html> <head> <title>Message Board</title> </head> <body> <h2>Welcome, <?php echo $_SESSION['username']; ?></h2> <form action="logout.php" method="post"> <input type="submit" value="Logout"> </form> <h3>Post a message:</h3> <form action="post_message.php" method="post"> <label>Name:</label> <input type="text" name="name"><br><br> <label>Email:</label> <input type="email" name="email"><br><br> <label>Message:</label> <textarea name="message" rows="3" cols="40"></textarea><br><br> <input type="submit" value="Post"> </form> <h3>Messages:</h3> <?php if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "<p><strong>" . $row["name"]. "</strong> (". $row["email"] ."): " . $row["message"]. " <br>[". $row["timestamp"] ."]</p>"; } } else { echo "No messages yet. Be the first one to leave a message!"; } ?> </body> </html> <?php $conn->close(); ?>
In this page, we get all the message information and display it on the page one by one. In addition, in order to facilitate users to log out, we have added a "Logout" button to the message board page. When the user clicks this button, all session variables will be destroyed and jump back to the login page.
4. Implementation of message submission
Finally, we need to implement a form processing program. In this program, we will obtain the message information submitted by the user and store it in the database, and then jump back Message board page and displays submitted messages.
The following is the source code of the message submission program:
<?php /*接收并处理留言*/ session_start(); /*获取表单提交信息*/ $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; /*数据库连接*/ $servername = "localhost"; $db_username = "root"; $db_password = ""; $dbname = "message_board"; $conn = new mysqli($servername, $db_username, $db_password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } /*存储留言*/ $sql = "INSERT INTO messages (name, email, message) VALUES ('$name', '$email', '$message')"; if ($conn->query($sql) === TRUE) { header("Location: message_board.php"); } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); ?>
This program will receive the message information and store it in the database, and jump back to the message board page. When the user returns to the message board page, it will automatically refresh the page and display new messages.
Summary
In this article, we use PHP language to implement a simple and easy-to-use login message board, and introduce the design and implementation of each page in detail. Of course, this is just a simple example. If you need to develop more complex websites or functions, you need to learn the PHP language and other related technologies in depth. However, by studying this example, you will be able to master the basic knowledge and skills of processing forms, database operations, and session management in PHP.
The above is the detailed content of How to log in to the message board in php. For more information, please follow other related articles on the PHP Chinese website!