Examples to explain how to implement password modification and exit page in PHP

PHPz
Release: 2023-04-03 20:16:02
Original
1092 people have browsed it

PHP is a widely used scripting language that is very suitable for web development and can also play its advantages in password modification and exit pages. This article will focus on how to implement password modification and exit pages in PHP.

1. Password modification page

When a user wants to change their password, they should first jump to a password modification page and let them enter the original password and new password. In PHP, you can use the $_POST variable to receive the data submitted by the user. The code is as follows:

<?php
session_start();
if(isset($_SESSION[&#39;username&#39;])) { //判断用户是否已登录
    if(isset($_POST[&#39;submit&#39;])) { //判断是否提交了表单
        $old_password = md5($_POST[&#39;old_password&#39;]); //将原密码加密
        $new_password = md5($_POST[&#39;new_password&#39;]); //将新密码加密
        //根据实际情况修改用户名和密码所对应的字段名
        $conn = mysqli_connect(&#39;localhost&#39;, &#39;root&#39;, &#39;123456&#39;, &#39;test&#39;);
        $sql = "SELECT username, password FROM users WHERE username=&#39;{$_SESSION[&#39;username&#39;]}&#39; AND password=&#39;{$old_password}&#39;";
        $result = mysqli_query($conn, $sql);
        if(mysqli_num_rows($result) == 1) { //如果查询结果为一条数据,说明原密码正确
            $sql = "UPDATE users SET password=&#39;{$new_password}&#39; WHERE username=&#39;{$_SESSION[&#39;username&#39;]}&#39;";
            $result = mysqli_query($conn, $sql);
            if($result) {
                echo &#39;密码修改成功!&#39;;
            } else {
                echo &#39;密码修改失败!&#39;;
            }
        } else {
            echo &#39;原密码错误!&#39;;
        }
    }
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>密码修改</title>
</head>
<body>
    <form action="" method="post">
        <label for="old_password">原密码:</label>
        <input type="password" name="old_password" id="old_password"><br>
        <label for="new_password">新密码:</label>
        <input type="password" name="new_password" id="new_password"><br>
        <input type="submit" name="submit" value="提交">
    </form>
</body>
</html>
Copy after login

In the above code, we first check whether the user is logged in. If not, it cannot change Password. Then receive the original password and new password submitted by the form through the $_POST variable, and compare the original password with the password saved in the database. If they are the same, update the password, otherwise it will prompt that the original password is wrong.

2. Exit page

In web applications, we usually provide an exit page to allow users to safely exit the current session. In PHP, we can use the session_destroy() function to destroy the current session and then jump to the login page. The code is as follows:

<?php
session_start();
session_destroy();
header(&#39;location: login.php&#39;);
?>
Copy after login

In the above code, we first start the session, then use the session_destroy() function to destroy the current session, and finally use the header() function Jump to the login page.

Summary

In this article, we learned how to implement password modification and exit pages in PHP. We can easily implement these functions through form submission and session handling. Of course, in actual applications, errors and security issues need to be handled more carefully to ensure the stability and security of the program.

The above is the detailed content of Examples to explain how to implement password modification and exit page 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