Php simple login and logout instance program (session) _ PHP Tutorial

Source: Internet
Author: User
Php simply logs in and out of the instance program (session ). Using a php instance to log on and log out, we usually use session to save information that records user logon success, then, when exiting, we can use unset to clear the session to enable user login and logout. using php instance login and logout, we generally use session to save information that records user logon success, after exiting, we can use unset to clear the session to enable user login and logout. here is a simple example.

About session processing

HTTP is a stateless protocol, indicating that the processing of each request is irrelevant to the previous or later requests. However, in order to adjust the user's unique behavior and preferences, there is a practice of storing a small amount of information (often called cookies) on the client. However, due to the limitation of the cookie size, the number of allowed cookies, and various implementations of cookies, another solution is session processing.

Session processing is implemented by assigning a unique identifier attribute called session ID (SID) to each website visitor, and then associating this SID with any amount of data.

Start Session

Session_start ();

Create session variables

The code is as follows:

$ _ SESSION ['username'] = "jason ";

Delete session variables

The code is as follows:
Unset ($ _ SESSION ['username']);

Simple login and logout

The code is as follows:

$ Supervisor = "admin ";
$ Superpsw = "passwd ";

// Check whether the form is submitted
If (isset ($ _ POST ['superadmin'])
If (! ($ _ POST ['supername'] = $ supervisor) |! ($ _ POST ['superpass'] = $ superpsw ))
Echo "incorrect user name or password ";
Session_start ();
$ _ SESSION ["superlogin"] = $ _ POST ['supername'];
} Else {
Session_start ();
// Check whether the session variable is set, that is, whether you have logged on. if not, the logon page is displayed.
If (! Isset ($ _ SESSION ["superlogin"])
Echo "";
// The user destroys session variables and logs out.
If (isset ($ _ GET ['logout']) {
Unset ($ _ SESSION ['superlogin']);
Header ("Location: index. php ");

Assume that you name this file include. php and include it to the page for login verification, such as index. php.

The code is as follows:

Require "include. php ";




Access index in this way. the php page displays the logon page, and the index is displayed after logon. php page content, this process continues until the user ends the session, such as closing the browser or clicking the logout button, but the session itself has a default lifetime on the PHP server.

The duration of valid sessions is controlled by php. ini. the default value is 1440 seconds, that is, 24 minutes.

Session. gc_maxlife time = 1440

PS: This article is an example. it uses simple code to describe how to use more complex control mechanisms in practical applications.


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: 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.