How to implement login and publish article functions in php

PHPz
Release: 2023-04-21 09:19:16
Original
939 people have browsed it

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)
Copy after login

);

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 for="username">Username</label>
<input type="text" name="username" required>

<label for="password">Password</label>
<input type="password" name="password" required>

<input type="submit" value="Login">
Copy after login

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);</p>
<p>}</p>
<p>//Get the username and password from the form<br>$username = $_POST["username"];<br>$password = $_POST[ "password"];</p>
<p>//Query database to query users<br>$sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";<br>$result = $conn->query($sql);</p>
<p>//If the user system exists, add it to the SESSION and then redirect to the homepage<br>if ($result->num_rows == 1) {</p>
<pre class="brush:php;toolbar:false">session_start();
$_SESSION["username"] = $username;
header("Location: index.php");
exit;
Copy after login

} else {

echo "Invalid username or password.";
Copy after login

}

$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)
Copy after login

);

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 for="title">Title</label>
<input type="text" name="title" required>

<label for="content">Content</label>
<textarea name="content" required></textarea>

<input type="submit" value="Save">
Copy after login

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);</p>
<p>}</p>
<p>//Get article information from the form<br>$title = $_POST["title"];<br>$content = $_POST[" content"];<br>$author = $_SESSION["username"];</p>
<p>//Save article information into the database<br>$sql = "INSERT INTO posts (title, content, author) VALUES ('$title', '$content', '$author')";<br>if ($conn->query($sql) === TRUE) {</p>
<pre class="brush:php;toolbar:false">header("Location: index.php");
exit;
Copy after login

} else {

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

}

$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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!