Home >php教程 >php手册 >ajax login jump login implementation

ajax login jump login implementation

伊谢尔伦
伊谢尔伦Original
2016-12-10 09:33:592147browse

When our users log in to the system, the username and password are verified by the backend. Moreover, the user login status also needs to be set on the backend. After querying the database, if the username and password are correct, a uuid will be stored in the session. When each page needs to judge and display the corresponding content based on the login status, it will be judged whether there is a uuid session. , and uuid is not empty. For example, the login button in the header displays the login button when not logged in, and displays the username and avatar after logging in. These are all used to determine whether there is a uuid value (in the specific implementation, it is relatively more complicated).

Let’s go through this step again:

Send the username and password to the backend ajax

After the backend receives the username and password, query the database

If the query fails, a json data will be returned, such as: {"status" :"-1", msg:"The username or password is incorrect"}

If the query is successful, store a value in the session, such as uuid, and then return a json to the front end, such as: {"status":"0 ", msg:"success"}

After the front-end js receives the returned data, it determines the value of status. If status==0, use window.location.href to jump; other status codes are login failures. Clear the password box and let the user re-enter

For example, if the login is successful and jumps to the homepage, php or java on the homepage needs to determine whether there is a uuid in the session

<nav>
    <?php if(isset($_SESSION[&#39;uuid&#39;]) && !empty($_SESSION[&#39;uuid&#39;])):;  ?>
        <!-- 若存在uuid,则不为空,则表示已经登录 -->
        <a href="#">蚊子</a>
    <?php else ?>
        <!-- 没有登录 -->
        <input type="button" value="登录" />
    <?php endif; ?>
</nav>


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