PHP is a very popular programming language in web development. It can be used to create various dynamic websites. Among them, implementing user login and publishing article functions is a very basic function, and it is also one of the introductory parts of learning PHP. This article will introduce how to use PHP to achieve these two functions.
- Implementing user login function
User login is a very basic function in web applications, allowing users to access and manage their account information by entering their username and password. The following are the steps to implement user login.
1.1 Create database
First, we need to create a table in the MySQL database to store user information. The following is the structure of an example table:
CREATE TABLE users (
id INT(11) NOT NULL AUTO_INERMENT, username VARCHAR(30) NOT NULL, password VARCHAR(255) NOT NULL, email VARCHAR(50) NOT NULL, PRIMARY KEY(id)
);
In this table, we use the id field as the primary key to store each user's Unique identifier. The user's username, password, and email address are also stored in the table.
1.2 Create a login form
Next, we need to create a login form that allows users to enter their username and password. When a user submits a form, we can use PHP to authenticate them.
The following is the code for a basic login form:
> ;In the above code, we define a form containing username and password input boxes, and specify the submission address of the form as "login.php".
1.3 Verify user identity
When a user submits a form, we need to use PHP to verify their identity. We can create a new file called "login.php" to handle login requests. The following is the code that needs to be in the file:
//Connect to the database
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn-> ;connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//Get the username and password from the form
$username = $_POST["username"];
$password = $_POST[ "password"];
//Query database to query users
$sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = $conn->query($sql);
//If the user system exists, add it to the SESSION and then redirect to the homepage
if ($result->num_rows == 1) {
session_start(); $_SESSION["username"] = $username; header("Location: index.php"); exit;
} else {
echo "Invalid username or password.";
}
$conn->close();
?>
in the above In the code, we first connect to the database, then get the input values in the form, and query the user in the database. If the user exists, add it to the SESSION for subsequent use, and then redirect to the home page. Otherwise, we will display an error message to the user.
- Implement the function of publishing articles
When users log in, they can publish new articles to the website. Here are the steps to implement this functionality.
2.1 Create article table
First, we need to create a new form in the database to store articles. The following is the structure of an example form:
CREATE TABLE posts (
id INT(11) NOT NULL AUTO_INERMENT, title VARCHAR(255) NOT NULL, content TEXT NOT NULL, author VARCHAR(30) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY(id)
);
In this table, we use the id field as the primary key to store the Unique identifier. The title, content, and author of the article are also stored in the table.
2.2 Create a publish article form
Next, we need to create a publish article form that allows users to enter the title and content of the article. When the user submits the form, we can use PHP to save the article information into the database.
The following is the code for a basic post form:
The above is the detailed content of How to implement login and publish article functions in php. 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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
