Home >Backend Development >PHP Tutorial >PHP transfers value to jump page after successful login

PHP transfers value to jump page after successful login

王林
王林Original
2024-03-12 12:42:031285browse

PHP transfers value to jump page after successful login

PHP jumps to the page after successful login

In web development, user login is a very basic and common function. After the user successfully logs into the system, it is usually necessary to pass the user's relevant information to the next page for subsequent operations or to display the user's personalized content. This article will demonstrate the specific code example of using PHP to pass a value and jump to the page after the user successfully logs in.

Implementation steps

1. Login page

First, we need to write a user login page (login.php) for users to enter their account number and password to log in. The following is a simple login page sample code:

<!DOCTYPE html>
<html>
<head>
    <title>用户登录</title>
</head>
<body>
    <h2>用户登录</h2>
    <form action="login_process.php" method="post">
        <label for="username">用户名:</label>
        <input type="text" name="username" id="username" required><br><br>
        <label for="password">密码:</label>
        <input type="password" name="password" id="password" required><br><br>
        <input type="submit" value="登录">
    </form>
</body>
</html>

2. Login processing page

Next, we need to write the PHP code (login_process.php) that handles user login and validates the user input Are the account number and password correct? If the login is successful, the user information is stored in the Session and jumps to the user information page (profile.php).

<?php
session_start();

// 假设以下为数据库验证逻辑,此处仅为示例
$username = $_POST['username'];
$password = $_POST['password'];

// 假设用户名为admin,密码为123456
if ($username === 'admin' && $password === '123456') {
    $_SESSION['username'] = $username;
    header('Location: profile.php');
} else {
    echo "登录失败,请检查用户名和密码";
}
?>

3. User information page

Finally, we write the user information page (profile.php), in which the user information stored in the Session is obtained and displayed. If the user is not logged in, jump back to the login page.

<?php
session_start();

if(isset($_SESSION['username'])) {
    $username = $_SESSION['username'];
    echo "欢迎,$username! 用户信息页面内容";
} else {
    header('Location: login.php');
}
?>

Summary

Through the above code example, we demonstrate the implementation method of passing a value and jumping to the page after the user successfully logs in. In actual projects, the code can be further expanded and optimized according to needs, such as adding database connection, encryption verification and other functions. Hope this article helps you!

The above is the detailed content of PHP transfers value to jump page after successful login. 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