"PHP additions and Deletions Instance" section 19th-use of session: Let the server know who you are?

Source: Internet
Author: User


Because an HTTP request is a stateless request, the so-called stateless state is that the server does not record the information you requested this time. HTTP It is a data transfer protocol based on the request-corresponding pattern. That is, you send a request, my server gives you a response, this thing is finished. No state is also a way of no memory.

The role of the session is to temporarily record some of your users ' data on the server side, so that users are called directly in subsequent operations.

Without session technology, users will need to re-authenticate their usernames and passwords regardless of the operation they do.

However, if the session is used, as long as the user's browser is not off, the server will be able to remember your identity information for subsequent operations to invoke.

The session life cycle is related to the browser, once the browser is closed, then the server will be cleared off this session.

In this system, how to use this session?

Open login2.php, at the end of the code, when the user login verification success, we put the user name into the session to ensure that the subsequent operation, the server considers the current login information is valid.

session_start(); // 使用session之前,我们必须要用session_start函数来开启session$_SESSION["username"] = $username;

Go back to login2.html, and change the URL of the successful login to: /main.php

Change the main.html suffix name here to. php

Open main.php

At the top of the file, add the session-enabled code.

And then in this place:

The super Administrator cannot write dead in the page, but should use PHP code to get the username in the session, paste to this place.

Replace the Super Administrator with the following code:

<?php  echo $_SESSION["username"]; ?>

Put the username attached to the session.

Now there is a problem, if the user is not logged in, you can also access to the Main.php page, which is not right.

How to determine whether the user is logged in? Is it just to see if the username exists or doesn't exist?

If there is no username in the session, then jump to the login screen.

 <script type="text/javascript">            <?php                if(!isset($_SESSION["username"])){                    echo "location.href=‘login/login2.html‘;";                }            ?>        </script>

SOURCE Access: HTTPS://WWW.JIANSHU.COM/P/4977BD0073D5

"PHP additions and Deletions Instance" section 19th-use of session: Let the server know who you are?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.