Home >Backend Development >PHP Tutorial >PHP implements the question author and answerer authentication function in the knowledge question and answer website.
PHP implements the question author and respondent authentication function in the knowledge question and answer website
In modern social networks, the popularity of knowledge question and answer websites is increasing day by day. In order to improve the credibility and user experience of the website, authenticating question authors and answerers is an important feature. This article will introduce how to use PHP to implement the question author and answerer authentication function in the knowledge question and answer website.
First, we need to create a user database and add corresponding fields to store user information. In this example, we assume that we have created a table named "users" with the following fields: id, username, email, password, is_author_verified, and is_answerer_verified.
Next, we need to create user registration and login pages. The registration page allows users to create new accounts, while the login page is used to log in for registered users. The following is a simple registration page example:
<!DOCTYPE html> <html> <head> <title>注册</title> </head> <body> <h2>注册</h2> <form method="post" action="register.php"> <input type="text" name="username" placeholder="用户名" required /><br><br> <input type="email" name="email" placeholder="邮箱" required /><br><br> <input type="password" name="password" placeholder="密码" required /><br><br> <input type="submit" value="注册" /> </form> </body> </html>
After the registration page submits the form, the data is sent to a back-end processing script named "register.php". The following is a simple example:
<?php // 连接数据库 $conn = new mysqli('localhost', 'username', 'password', 'database'); // 获取表单数据 $username = $_POST['username']; $email = $_POST['email']; $password = $_POST['password']; // 加密密码 $hashed_password = password_hash($password, PASSWORD_DEFAULT); // SQL 插入语句 $sql = "INSERT INTO users (username, email, password) VALUES ('$username', '$email', '$hashed_password')"; // 执行插入操作 $conn->query($sql); // 关闭数据库连接 $conn->close(); // 返回登录页面 header("Location: login.php"); exit(); ?>
It should be noted that in actual development, user input should be verified and SQL injection prevented.
The following is an example of a login page:
<!DOCTYPE html> <html> <head> <title>登录</title> </head> <body> <h2>登录</h2> <form method="post" action="login.php"> <input type="email" name="email" placeholder="邮箱" required /><br><br> <input type="password" name="password" placeholder="密码" required /><br><br> <input type="submit" value="登录" /> </form> </body> </html>
The form of the login page submits data to a back-end processing script named "login.php". The following is a simple example:
<?php // 连接数据库 $conn = new mysqli('localhost', 'username', 'password', 'database'); // 获取表单数据 $email = $_POST['email']; $password = $_POST['password']; // 查询匹配的用户 $sql = "SELECT * FROM users WHERE email='$email'"; $result = $conn->query($sql); // 验证密码 $user = $result->fetch_assoc(); if ($user && password_verify($password, $user['password'])) { // 登录成功,将用户信息保存在 session 中 session_start(); $_SESSION['user_id'] = $user['id']; $_SESSION['username'] = $user['username']; $_SESSION['email'] = $user['email']; $_SESSION['is_author_verified'] = $user['is_author_verified']; $_SESSION['is_answerer_verified'] = $user['is_answerer_verified']; // 跳转到首页或其他页面 header("Location: index.php"); exit(); } else { // 登录失败,返回登录页 header("Location: login.php"); exit(); } ?>
After successful login, we save the user information in the session for subsequent page access and authentication control.
In order to implement the question author and respondent authentication function, we can add corresponding options to the user profile page. The following is a simple profile page example:
<!DOCTYPE html> <html> <head> <title>个人资料</title> </head> <body> <h2>个人资料</h2> <p>用户名:<?php echo $_SESSION['username']; ?></p> <p>邮箱:<?php echo $_SESSION['email']; ?></p> <form method="post" action="verify.php"> <input type="checkbox" name="is_author" <?php if($_SESSION['is_author_verified']) echo 'checked'; ?>> 作者<br> <input type="checkbox" name="is_answerer" <?php if($_SESSION['is_answerer_verified']) echo 'checked'; ?>> 回答者<br> <input type="submit" value="保存" /> </form> </body> </html>
In the profile page, we display the user's username and email, and use check boxes to allow the user to choose whether to be certified as an author or respondent. After the form is submitted, the data is sent to a backend processing script called "verify.php". The following is a simple example:
<?php // 连接数据库 $conn = new mysqli('localhost', 'username', 'password', 'database'); // 获取表单数据 $is_author = isset($_POST['is_author']) ? 1 : 0; $is_answerer = isset($_POST['is_answerer']) ? 1 : 0; // 更新用户认证信息 $user_id = $_SESSION['user_id']; $sql = "UPDATE users SET is_author_verified=$is_author, is_answerer_verified=$is_answerer WHERE id=$user_id"; $conn->query($sql); // 更新 session $_SESSION['is_author_verified'] = $is_author; $_SESSION['is_answerer_verified'] = $is_answerer; // 关闭数据库连接 $conn->close(); // 返回个人资料页面 header("Location: profile.php"); exit(); ?>
The above code demonstrates how to use PHP to implement the question author and respondent authentication functions in a knowledge question and answer website. With registration, login, profile pages, and authentication controls, we can improve the trustworthiness and user experience of your trivia website. Of course, more functions and security mechanisms can be added based on actual needs.
The above is the detailed content of PHP implements the question author and answerer authentication function in the knowledge question and answer website.. For more information, please follow other related articles on the PHP Chinese website!