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.
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);</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;
} 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.
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: