Php allows users to log on to simple instances and php users to log on to instances.

Source: Internet
Author: User

Php allows users to log on to simple instances and php users to log on to instances.

Php allows users to log on to simple instances

Preface:

The last part to be completed recently is to manage the data submitted by the user. As for management, it must be the administrator. It must involve login and account permission verification, whether the account has expired or not.

Required knowledge

Session is indeed a very important thing. In addition, the session cannot span pages. Modify the PHP. ini session. use_trans_sid = 0 to 1.

Implementation

My background design is relatively simple. You only need to enter a password. Of course, this password can be changed on the server. Therefore, you only need to add two variables, flag and time, to the session.

First, use flag to determine whether the Administrator has successfully logged on, and use time to determine whether the logon times out. After the password is submitted, if it is correct, the flag is assigned a value of 1, and the time value is the current time. Each time you enter a new page or perform operations, the two variables will be judged. First, determine whether the flag value is 1. If it is not unique, the system will directly prompt that the session is not logged in and destroy the session. If it is 1, then judge whether the current time-$ _ SESSION ('time') is less than 600 (10 minutes). If the value is greater than, the system prompts that the logon times out and the session is destroyed. If the value is less than, the operation is allowed, and update the time variable value to the current value.

Some code

Check_pw.php

<? Phpsession_start (); $ _ SESSION ['flag'] = 0; $ myfile = fopen ("passwd", "r") or die ("Unable to open file! "); $ Passwd = fgets ($ myfile); if (empty ($ _ POST ['pass']) {echo" cannot be blank, re-enter "; $ page = "login.html";} else {$ pass = $ _ POST ['pass']; $ passwd = test_input ($ passwd); $ pass = test_input ($ pass ); if ($ pass = $ passwd) {echo "the password is correct and access is allowed"; $ page = "list_all.php"; $ _ SESSION ['flag'] = 1; $ _ SESSION ['time'] = time (); // current number of seconds} else {echo "Incorrect password, re-enter"; $ page = "login.html ";}} function test_input ($ date) {$ date = trim ($ date) ; $ Date = stripcslashes ($ date); $ date = htmlspecialchars ($ date); return $ date ;}?> <A href = "<? Php echo $ page;?> "Rel =" external nofollow "> click here to jump </a>

List_all.php (Part)

<? Phpsession_start (); if ($ _ SESSION ['flag'] = 1) {if (time ()-$ _ SESSION ['time']> 600) {echo "Login timeout"; echo '<a href = "login.html" rel = "external nofollow" rel = "external nofollow"> click here to log on </a> '; session_destroy (); exit () ;}else {$ _ SESSION ['time'] = time () ;}} else {echo "is not logged in and has no access permission! "; Echo '<a href =" login.html "rel =" external nofollow "rel =" external nofollow "> click here to log on </a>'; session_destroy (); exit () ;}?>

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

Related Article

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.