Home > Backend Development > PHP Tutorial > PHP simple login and logout example program (session)_PHP tutorial

PHP simple login and logout example program (session)_PHP tutorial

WBOY
Release: 2016-07-13 10:49:33
Original
1090 people have browsed it

Using php instances to log in and log out, we usually use sessions to save and record the information of successful user logins, and then use unset to clear the session when logging out to realize the user login and logout functions. Let me introduce a simple instance.

About session processing

HTTP is a stateless protocol, meaning that the processing of each request has nothing to do with previous or subsequent requests. However, in order to be able to adjust user-specific behaviors and preferences, a method has emerged to store a small amount of information on the client ( (often called cookies), but due to cookie size limitations, the number of cookies allowed, and various inconsistencies in cookie implementation, another solution has emerged: session processing.

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

Start conversation

session_start();

Create session variables

The code is as follows Copy code
 代码如下 复制代码

$_SESSION['username'] = “jason”;

$_SESSION['username'] = "jason";

Delete session variables
 代码如下 复制代码
unset($_SESSION['username']);

Simple login and logout
 代码如下 复制代码

$supervisor = "admin";
$superpsw = "passwd";

// 检查是否提交表单
if (isset($_POST['superadmin']))
{
if (!($_POST['supername'] == $supervisor) || !($_POST['superpass'] == $superpsw))
{
echo "用户名或密码错误";
exit;
}
else
{
session_start();
$_SESSION["superlogin"] = $_POST['supername'];
}
} else {
session_start();
// 检查是否设置了会话变量,即是否已经登入,如果没有,显示登入页面
if (! isset($_SESSION["superlogin"]) )
{
echo "

";
echo "
请输入管理员密码
";
echo "管理员";
echo "
";
echo "密  码";
echo "
";
echo "
";
echo "
";
echo "
";
exit;
}
}
// 由用户销毁会话变量,登出
if (isset($_GET['logout'])) {
unset($_SESSION['superlogin']);
header("Location:index.php");
}

The code is as follows Copy code
$supervisor = "admin";
$superpsw = "passwd"; // Check whether the form is submitted
if (isset($_POST['superadmin']))
{
if (!($_POST['supername'] == $supervisor) || !($_POST['superpass'] == $superpsw))
{
echo "Wrong username or password";
exit;
}
else
{
session_start();
$_SESSION["superlogin"] = $_POST['supername'];
}
} else {
session_start();
// Check whether the session variable is set, that is, whether you have logged in, if not, display the login page
if (! isset($_SESSION["superlogin"]) )
{
echo "
";
echo "
Please enter the administrator password
";
echo "Administrator";
echo "
";
echo "password";
echo "
";
echo "
";
echo "
";
echo "
";
exit;
}
}
// Destroy session variables by user and log out
if (isset($_GET['logout'])) {
unset($_SESSION['superlogin']);
header("Location:index.php");
}

Assume this file is named include.php and include it to the page where you want to verify login, such as index.php

The code is as follows
 代码如下 复制代码

    require “include.php”;
?>
   
   
    管理
   
   


   
   

登出
   

欢迎进入


   
   

Copy code

​ require “include.php”;
?>


Management



Logout

Welcome



When you access the index.php page in this way, you will enter the login page. After logging in, the content of the index.php page will be displayed. 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.

The duration of a valid session is controlled by php.ini, and the default is 1440 seconds, which is 24 minutes
session.gc_maxlifetime = 1440

PS: This article is an example, using simple code to illustrate. In actual applications, more complex control mechanisms will be used. http://www.bkjia.com/PHPjc/632693.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/632693.html
TechArticle
Use php instance to log in and out. We usually use session to save and record the information of successful user login, and then log out. We then use unset to clear the session to realize user login and logout...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template