search
HomeBackend DevelopmentPHP ProblemHow to implement login and publish article functions in php

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.

  1. 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:

<label>Username</label>
<input>

<label>Password</label>
<input>

<input>

> ;

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.

  1. 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:

<label>Title</label>
<input>

<label>Content</label>
<textarea></textarea>

<input>

form>

In the above code, we define a form containing the article title and content input box, and specify the submission address of the form as "save_post.php".

2.3 Save article information to the database

When the user submits the form, we need to use PHP to save the article information to the database. We can create a new file called "save_post.php" to handle this request. 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 article information from the form
$title = $_POST["title"];
$content = $_POST[" content"];
$author = $_SESSION["username"];

//Save article information into the database
$sql = "INSERT INTO posts (title, content, author) VALUES ('$title', '$content', '$author')";
if ($conn->query($sql) === TRUE) {

header("Location: index.php");
exit;

} else {

echo "Error: " . $sql . "<br>" . $conn->error;

}

$conn->close();
?>

In the above code, we first connect to the database, then get the article information in the form and save them to the database. If the article is stored successfully, it will redirect to the home page. Otherwise, we will display an error message to the user.

Summary

In this article, we introduced how to use PHP to implement user login and article publishing functions. We first created a new database table to store user information and article information, and then created the corresponding login and article publishing forms. Finally, we use PHP to verify user login information and save article information to the database. Implementing these basic functions is an important step in learning PHP programming, and I hope it will be helpful to beginners.

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

MantisBT

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

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

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser

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

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.