Home >Backend Development >PHP Tutorial >PHP simple login and logout example program (session)_PHP tutorial
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 | ||||
|
代码如下 | 复制代码 |
unset($_SESSION['username']); |
Simple login and logout
代码如下 | 复制代码 |
$supervisor = "admin"; // 检查是否提交表单 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 ""; 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
|
Copy code
|
||||
require “include.php”; 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 |
http: //www.bkjia.com/PHPjc/632693.html