本文要為大家介紹PHP會(huì)話如何指定時(shí)間銷毀的問題,以下就給大家詳細(xì)介紹如何透過session_destroy()這個(gè)函數(shù)來(lái)銷毀會(huì)話的,希望對(duì)需要的朋友有所幫助~
#PHP有一個(gè)核心函數(shù)session_destroy()來(lái)清除所有會(huì)話值。它是一個(gè)簡(jiǎn)單的沒有參數(shù)的函數(shù),傳回一個(gè)布林值true或false。
PHP的會(huì)話ID預(yù)設(shè)儲(chǔ)存在一個(gè)cookie中。一般來(lái)說(shuō),該會(huì)話cookie檔案的名字是PHPSESSID。 session_destroy函數(shù)不會(huì)取消cookie中的sessionid。
為了 "完全 "銷毀會(huì)話,會(huì)話ID也必須被取消設(shè)定。
這個(gè)快速的範(fàn)例使用session_destroy()來(lái)銷毀會(huì)話。它使用set_cookie()方法,透過過期的PHP會(huì)話ID來(lái)殺死整個(gè)會(huì)話。
快速範(fàn)例
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會(huì)話銷毀後重新啟動(dòng)會(huì)話。
使用PHP$_SESSION取消設(shè)定特定的會(huì)話變數(shù)。對(duì)於較舊的PHP版本,請(qǐng)使用session_unset()。
php會(huì)話銷毀輸出【推薦學(xué)習(xí):PHP影片教學(xué)】
關(guān)於此登入session_destory()範(fàn)例
讓我們建立一個(gè)登入範(fàn)例程式碼,以使用PHP會(huì)話、 session_destroy等。它允許用戶從當(dāng)前會(huì)話登入和登出。如果您在PHP腳本中尋找完整的使用者註冊(cè)和登錄,請(qǐng)使用此程式碼。
此範(fàn)例提供了自動(dòng)登入會(huì)話到期功能。
帶有登入表單的登入頁(yè)面
此表單發(fā)佈使用者輸入的使用者名稱和密碼。它驗(yàn)證PHP中的登入憑證。
成功登入後,它將登入狀態(tài)儲(chǔ)存到PHP會(huì)話。它將過期時(shí)間設(shè)定為從上次登入時(shí)間起30分鐘。
它將上次登入時(shí)間和到期時(shí)間儲(chǔ)存到PHP會(huì)話。這兩個(gè)會(huì)話變數(shù)用於使會(huì)話自動(dòng)過期。
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>
儀表板驗(yàn)證PHP登入工作階段並顯示登入和登出連結(jié)
這是登入後重新導(dǎo)向的目標(biāo)頁(yè)面。如果登入會(huì)話存在,它將顯示註銷連結(jié)。
一旦超時(shí),它將呼叫銷毀會(huì)話。 php程式碼來(lái)銷毀所有會(huì)話。
如果達(dá)到30分鐘到期時(shí)間或會(huì)話為空,它會(huì)要求使用者登入。
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程式碼用於希望在會(huì)話到期前登出的使用者。
它透過要求銷毀會(huì)話來(lái)銷毀會(huì)話。 php代碼。然後,它將使用者重新導(dǎo)向到登入頁(yè)面。
logout.php
<?php
session_start();
require_once __DIR__ . '/destroy-session.php';
header('Location: login.php');
?>
我希望這個(gè)範(fàn)例有助於理解如何銷毀PHP會(huì)話。而且,這是一個(gè)完美的場(chǎng)景,適合解釋銷毀會(huì)話的必要性。
以上是詳解PHP會(huì)話如何實(shí)現(xiàn)在30分鐘後被銷毀(附程式碼實(shí)例)的詳細(xì)內(nèi)容。更多資訊請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!