PHP Simple login Log Out instance program (session)

Source: Internet
Author: User

About session Processing

HTTP is a stateless protocol that shows that each request is handled independently of the previous or subsequent request, but in order to be able to adjust user-specific behavior and preferences, there is a practice of storing small amounts of information (often called cookies) on the client, but due to the size of the cookie, the allowed The number of cookies and the various inconsistencies in the implementation of the cookie, there is another solution: Session processing.

Session processing is implemented by assigning each site visitor a unique identity attribute called a session ID (SID), and then associating this SID with any number of data.

Start session

Session_Start ();

To create a session variable

The code is as follows Copy Code

$_session[' username '] = "Jason";

To delete a session variable

The code is as follows Copy Code
unset ($_session[' username '));

Simple login Log Out

The code is as follows Copy Code

$supervisor = "admin";
$SUPERPSW = "passwd";

Check whether to submit the form
if (Isset ($_post[' superadmin '))
{
if (!) ( $_post[' supername '] = = $supervisor) | | ! ($_post[' superpass '] = = $SUPERPSW))
{
echo "User name or password error";
Exit
}
Else
{
Session_Start ();
$_session["Superlogin"] = $_post[' supername '];
}
} else {
Session_Start ();
Check to see if the session variable is set, that is, if it is logged in, and if not, display the login page
if (! isset ($_session["Superlogin"])
{
echo "<form name= ' Form1 ' method= ' post ' action= ' $_server[php_self] ' >";
echo "<div align= ' center ' > Please enter admin password <br/>";
echo "admin";
echo "<input type= ' text ' name= ' supername ' ><br/>";
echo "Password";
echo "<input type= ' password ' name= ' superpass ' ><br/>";
echo "<input type= ' submit ' name= ' superadmin ' value= ' into ' ><br/> '";
echo "<input type= ' reset ' name= ' Cancel ' value= ' rewrite ' ></div>";
echo "</form>";
Exit
}
}
The user destroys the session variable, logging out
if (Isset ($_get[' logout ')) {
unset ($_session[' superlogin '));
Header ("Location:index.php");
}

Suppose you name this file include.php and include it in the page where you want to verify the login, such as index.php

  code is as follows copy code
    <?php
    require "include.php";
   
   
   
     <title> management </title>
    <meta http-equiv= "Content-type content=" text/html; Charset=utf-8″>
   
    <body>
    <p><a href= "index.php?logout=1″> log out </a>
    <p> Welcome to </p>
     </body>
   

When you visit the index.php page, you will enter the login page, display the index.php page content after the login, and the 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 in the PHP server.

The duration of a valid session is controlled by php.ini and defaults to 1440 seconds, or 24 minutes.

Session.gc_maxlifetime = 1440

PS: This article is an example, with simple code to illustrate that the actual application will use more complex control mechanism.

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.