Home  >  Article  >  Backend Development  >  Front and backend php5 build complete file source code

Front and backend php5 build complete file source code

PHPz
PHPzOriginal
2023-03-29 10:11:22643browse

Many websites require a complete backend management system to manage data, update content and interact with users. To implement a complete backend management system, you need to use the PHP language.

This article will introduce how to build a complete backend management system through PHP5 language, and comes with complete file source code.

First of all, we need to understand what the PHP5 language is. PHP5 is a server-side scripting language that can be used to create dynamic web pages. It is open source, easy to learn and use, and runs on various platforms. PHP5 supports integration with databases, so it is very suitable for developing complete backend management systems.

Before we start writing code, we need to prepare some tools and software. First you need a web server such as Apache or Nginx. Secondly, a database is required, such as MySQL or SQLite. Finally you need a text editor such as Sublime Text or Notepad.

We will use PHP5 language to write the following functions:

  • Register a new user
  • User login
  • Modify user information
  • Upload files
  • Manage files
  • Delete files

The following is the complete file source code:

<?php

// 连接数据库
$link = mysqli_connect("localhost", "root", "password", "database_name");

// 注册新用户
function register_user($username, $password) {
  global $link;
  $username = mysqli_real_escape_string($link, $username);
  $password = mysqli_real_escape_string($link, $password);
  $password = password_hash($password, PASSWORD_DEFAULT);
  $sql = "INSERT INTO users (username, password) VALUES (&#39;$username&#39;, &#39;$password&#39;)";
  mysqli_query($link, $sql);
}

// 用户登录
function login_user($username, $password) {
  global $link;
  $username = mysqli_real_escape_string($link, $username);
  $password = mysqli_real_escape_string($link, $password);
  $sql = "SELECT * FROM users WHERE username=&#39;$username&#39;";
  $result = mysqli_query($link, $sql);
  if (mysqli_num_rows($result) == 1) {
    $row = mysqli_fetch_assoc($result);
    if (password_verify($password, $row[&#39;password&#39;])) {
      $_SESSION[&#39;user&#39;] = $row[&#39;id&#39;];
      return true;
    }
  }
  return false;
}

// 修改用户信息
function update_user($username, $password) {
  global $link;
  $username = mysqli_real_escape_string($link, $username);
  $password = mysqli_real_escape_string($link, $password);
  $password = password_hash($password, PASSWORD_DEFAULT);
  $id = $_SESSION[&#39;user&#39;];
  $sql = "UPDATE users SET username=&#39;$username&#39;, password=&#39;$password&#39; WHERE id=&#39;$id&#39;";
  mysqli_query($link, $sql);
}

// 上传文件
function upload_file($filename, $filepath) {
  global $link;
  $filename = mysqli_real_escape_string($link, $filename);
  $filepath = mysqli_real_escape_string($link, $filepath);
  $user_id = $_SESSION[&#39;user&#39;];
  $sql = "INSERT INTO files (filename, filepath, user_id) VALUES (&#39;$filename&#39;, &#39;$filepath&#39;, &#39;$user_id&#39;)";
  mysqli_query($link, $sql);
}

// 管理文件
function manage_files() {
  global $link;
  $user_id = $_SESSION[&#39;user&#39;];
  $sql = "SELECT * FROM files WHERE user_id=&#39;$user_id&#39;";
  $result = mysqli_query($link, $sql);
  while ($row = mysqli_fetch_assoc($result)) {
    echo "<tr><td>{$row['filename']}</td><td>{$row['filepath']}</td><td><a href=&#39;delete_file.php?id={$row[&#39;id&#39;]}&#39;>Delete</a></td></tr>";
  }
}

// 删除文件
function delete_file($id) {
  global $link;
  $id = mysqli_real_escape_string($link, $id);
  $user_id = $_SESSION['user'];
  $sql = "DELETE FROM files WHERE id='$id' AND user_id='$user_id'";
  mysqli_query($link, $sql);
}
?>

The above code uses the mysqli extension function, which provides Secure data processing and MySQL data access capabilities. It should be noted that some global variables are used in the code, such as $link and $_SESSION. These variables need to be defined or initialized in other parts of the code.

Functions such as registering new users, logging in, modifying user information, uploading files, managing files, and deleting files are all explained in detail in the code. Developers can freely modify and adjust according to their own needs.

Summary: This article introduces how to use PHP5 language to build a complete backend management system, and comes with complete file source code. Security and performance need to be paid attention to during development to ensure the correctness and reliability of the code.

The above is the detailed content of Front and backend php5 build complete file source code. 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