


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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
