本文給大家介紹有關PHP會話如何指定時間銷毀的問題,下面就給大家詳細介紹如何通過session_destroy()這個函數(shù)來銷毀會話的,希望對需要的朋友有所幫助~
PHP有一個核心函數(shù)session_destroy()來清除所有會話值。它是一個簡單的沒有參數(shù)的函數(shù),返回一個布爾值true或false。
PHP的會話ID默認存儲在一個cookie中。一般來說,該會話cookie文件的名字是PHPSESSID。session_destroy函數(shù)不會取消cookie中的sessionid。
為了 "完全 "銷毀會話,會話ID也必須被取消設置。
這個快速的例子使用session_destroy()來銷毀會話。它使用set_cookie()方法,通過過期的PHP會話ID來殺死整個會話。
快速例子
destroy-session.php
<?php
// Always remember to initialize the session,
// even before attempting to destroy it.
// Destroy all the session variables.
$_SESSION = array();
// delete the session cookie also to destroy the session
if (ini_get("session.use_cookies")) {
$cookieParam = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000, $cookieParam["path"], $cookieParam["domain"], $cookieParam["secure"], $cookieParam["httponly"]);
}
// as a last step, destroy the session.
session_destroy();
注:
使用session_start()在PHP會話銷毀后重新啟動會話。
使用PHP$_SESSION取消設置特定的會話變量。對于較舊的PHP版本,請使用session_unset()。
php會話銷毀輸出【推薦學習:PHP視頻教程】
關于此登錄session_destory()示例
讓我們創(chuàng)建一個登錄示例代碼,以使用PHP會話、session_destroy等。它允許用戶從當前會話登錄和注銷。如果您在PHP腳本中尋找完整的用戶注冊和登錄,請使用此代碼。
此示例提供了自動登錄會話到期功能。
帶有登錄表單的登錄頁
此表單發(fā)布用戶輸入的用戶名和密碼。它驗證PHP中的登錄憑據(jù)。
成功登錄后,它將登錄狀態(tài)存儲到PHP會話中。它將過期時間設置為從上次登錄時間起30分鐘。
它將上次登錄時間和到期時間存儲到PHP會話中。這兩個會話變量用于使會話自動過期。
login.php
<?php
session_start();
$expirtyMinutes = 1;
?>
<html>
<head>
<title>PHP Session Destroy after 30 Minutes</title>
<link rel='stylesheet' href='style.css' type='text/css' />
<link rel='stylesheet' href='form.css' type='text/css' />
</head>
<body>
<div class="phppot-container">
<h1>Login</h1>
<form name="login-form" method="post">
<table>
<tr>
<td>Username</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td><input type="submit" value="Sign in"
name="submit"></td>
</tr>
</table>
</form>
<?php
if (isset($_POST['submit'])) {
$usernameRef = "admin";
$passwordRef = "test";
$username = $_POST['username'];
$password = $_POST['password'];
// here in this example code focus is session destroy / expiry only
// refer for registration and login code https://phppot.com/php/user-registration-in-php-with-login-form-with-mysql-and-code-download/
if ($usernameRef == $username && $passwordRef == $password) {
$_SESSION['login-user'] = $username;
// login time is stored as reference
$_SESSION['ref-time'] = time();
// Storing the logged in time.
// Expiring session in 30 minutes from the login time.
// See this is 30 minutes from login time. It is not 'last active time'.
// If you want to expire after last active time, then this time needs
// to be updated after every use of the system.
// you can adjust $expirtyMinutes as per your need
// for testing this code, change it to 1, so that the session
// will expire in one minute
// set the expiry time and
$_SESSION['expiry-time'] = time() + ($expirtyMinutes * 60);
// redirect to home
// do not include home page, it should be a redirect
header('Location: home.php');
} else {
echo "Wrong username or password. Try again!";
}
}
?>
</div>
</body>
</html>
儀表板驗證PHP登錄會話并顯示登錄和注銷鏈接
這是登錄后重定向的目標頁面。如果登錄會話存在,它將顯示注銷鏈接。
一旦超時,它將調(diào)用銷毀會話。php代碼來銷毀所有會話。
如果達到30分鐘到期時間或會話為空,它會要求用戶登錄。
home.php
<?php
session_start();
?>
<html>
<head>
<title>PHP Session Destroy after 30 Minutes</title>
<link rel='stylesheet' href='style.css' type='text/css' />
<link rel='stylesheet' href='form.css' type='text/css' />
</head>
<body>
<div class="phppot-container">
<?php
if (! isset($_SESSION['login-user'])) {
echo "Login again!<br><br>";
echo "<a href='login.php'>Login</a>";
} else {
$currentTime = time();
if ($currentTime > $_SESSION['expiry-time']) {
require_once __DIR__ . '/destroy-session.php';
echo "Session expired!<br><br><a href='login.php'>Login</a>";
} else {
?>
<h1>Welcome <?php echo $_SESSION['login-user'];?>!</h1>
<a href='logout.php'>Log out</a>
<?php
}
}
?>
</div>
</body>
</html>
此PHP代碼用于希望在會話到期前注銷的用戶。
它通過要求銷毀會話來銷毀會話。php代碼。然后,它將用戶重定向到登錄頁面。
logout.php
<?php
session_start();
require_once __DIR__ . '/destroy-session.php';
header('Location: login.php');
?>
我希望這個示例有助于理解如何銷毀PHP會話。而且,這是一個完美的場景,適合解釋銷毀會話的必要性。