Home >Backend Development >PHP Tutorial >PHP implements user expansion and identity authentication functions in the knowledge question and answer website.

PHP implements user expansion and identity authentication functions in the knowledge question and answer website.

WBOY
WBOYOriginal
2023-07-03 23:54:131271browse

PHP implements user extension and identity authentication functions in a knowledge question and answer website

As a developer of a knowledge question and answer website, we often need to combine user identity authentication with user extension functions. This article will introduce how to use PHP to implement user expansion and authentication functions in a knowledge question and answer website, and provide some code examples.

User expansion refers to users’ improvement and personal customization of their own information on the website. Generally, users can create and manage their own accounts by registering and logging in. Identity authentication refers to verifying whether the user's identity is legitimate through a certain method.

Before we start, we need to build a simple website and set up a database connection. We can use MySQL as a database management system and connect and interact with the database through PHP's mysqli extension.

First, let’s implement the user registration function. The following is a simple registration form:

<form action="register.php" method="post">
  <input type="text" name="username" placeholder="用户名" required><br>
  <input type="password" name="password" placeholder="密码" required><br>
  <input type="email" name="email" placeholder="电子邮箱" required><br>
  <input type="submit" value="注册">
</form>

In the register.php file, we can get the data submitted by the form and store it in the database:

<?php
  // 获取表单提交的数据
  $username = $_POST['username'];
  $password = $_POST['password'];
  $email = $_POST['email'];

  // 将数据存储到数据库中
  $conn = mysqli_connect('localhost', 'root', 'password', 'database');
  $query = "INSERT INTO users (username, password, email) VALUES ('$username', '$password', '$email')";
  mysqli_query($conn, $query);
  mysqli_close($conn);

  // 注册成功后的跳转页面
  header('Location: login.php');
?>

Next, let’s implement User login function. The following is a simple login form:

<form action="login.php" method="post">
  <input type="text" name="username" placeholder="用户名" required><br>
  <input type="password" name="password" placeholder="密码" required><br>
  <input type="submit" value="登录">
</form>

In the login.php file, we can query whether there is a matching username and password in the database:

<?php
  // 获取表单提交的数据
  $username = $_POST['username'];
  $password = $_POST['password'];

  // 查询数据库中是否存在匹配的用户名和密码
  $conn = mysqli_connect('localhost', 'root', 'password', 'database');
  $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
  $result = mysqli_query($conn, $query);

  if (mysqli_num_rows($result) > 0) {
    // 用户名和密码验证通过,创建会话保存用户信息
    session_start();
    $_SESSION['username'] = $username;

    // 登录成功后的跳转页面
    header('Location: profile.php');
  } else {
    // 登录失败的处理逻辑
    echo '用户名或密码错误';
  }

  mysqli_close($conn);
?>

Finally, let’s implement the user expansion function . For example, users can complete and modify personal information on the profile page. The following is an example profile page:

<?php
  session_start();
  if (!isset($_SESSION['username'])) {
    // 用户未登录的处理逻辑
    header('Location: login.php');
  }
?>

<h1>个人资料</h1>
<p>欢迎,<?php echo $_SESSION['username']; ?>!</p>

<form action="update_profile.php" method="post">
  <input type="text" name="fullName" placeholder="姓名" required><br>
  <input type="text" name="address" placeholder="地址" required><br>
  <input type="submit" value="保存">
</form>

In the update_profile.php file, we can get the data submitted by the form and update the corresponding record in the database:

<?php
  session_start();
  if (!isset($_SESSION['username'])) {
    // 用户未登录的处理逻辑
    header('Location: login.php');
  }

  // 获取表单提交的数据
  $username = $_SESSION['username'];
  $fullName = $_POST['fullName'];
  $address = $_POST['address'];

  // 更新数据库中的对应记录
  $conn = mysqli_connect('localhost', 'root', 'password', 'database');
  $query = "UPDATE users SET fullName='$fullName', address='$address' WHERE username='$username'";
  mysqli_query($conn, $query);
  mysqli_close($conn);

  // 更新成功后的跳转页面
  header('Location: profile.php');
?>

With the above code example, We can implement user expansion and identity authentication functions in the knowledge question and answer website. Users can create and manage their own accounts by registering and logging in, and can complete and modify personal information on the profile page.

It should be noted that the above example is a simple demonstration. In actual application, issues such as security and scalability need to be considered, and appropriate verification and filtering mechanisms must be added to prevent malicious attacks and illegal operate.

During the development process, we can make appropriate modifications and adjustments according to actual needs to achieve functions that are more in line with project requirements. I hope this article can be helpful to you when implementing user expansion and identity authentication functions in your knowledge question and answer website.

The above is the detailed content of PHP implements user expansion and identity authentication functions in the knowledge question and answer website.. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn