溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

PHP會(huì)話怎么實(shí)現(xiàn)在30分鐘后被銷毀

發(fā)布時(shí)間:2022-11-15 09:32:52 來源:億速云 閱讀:100 作者:iii 欄目:編程語言

本篇內(nèi)容主要講解“PHP會(huì)話怎么實(shí)現(xiàn)在30分鐘后被銷毀”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“PHP會(huì)話怎么實(shí)現(xiàn)在30分鐘后被銷毀”吧!

PHP有一個(gè)核心函數(shù)session_destroy()來清除所有會(huì)話值。它是一個(gè)簡單的沒有參數(shù)的函數(shù),返回一個(gè)布爾值true或false。

PHP的會(huì)話ID默認(rèn)存儲(chǔ)在一個(gè)cookie中。一般來說,該會(huì)話cookie文件的名字是PHPSESSID。session_destroy函數(shù)不會(huì)取消cookie中的sessionid。

為了 "完全 "銷毀會(huì)話,會(huì)話ID也必須被取消設(shè)置。

這個(gè)快速的例子使用session_destroy()來銷毀會(huì)話。它使用set_cookie()方法,通過過期的PHP會(huì)話ID來殺死整個(gè)會(huì)話。

快速例子

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ì)話變量。對于較舊的PHP版本,請使用session_unset()。 php會(huì)話銷毀輸出

關(guān)于此登錄session_destory()示例

讓我們創(chuàng)建一個(gè)登錄示例代碼,以使用PHP會(huì)話、session_destroy等。它允許用戶從當(dāng)前會(huì)話登錄和注銷。如果您在PHP腳本中尋找完整的用戶注冊和登錄,請使用此代碼。 此示例提供了自動(dòng)登錄會(huì)話到期功能。

帶有登錄表單的登錄頁

此表單發(fā)布用戶輸入的用戶名和密碼。它驗(yàn)證PHP中的登錄憑據(jù)。 成功登錄后,它將登錄狀態(tài)存儲(chǔ)到PHP會(huì)話中。它將過期時(shí)間設(shè)置為從上次登錄時(shí)間起30分鐘。 它將上次登錄時(shí)間和到期時(shí)間存儲(chǔ)到PHP會(huì)話中。這兩個(gè)會(huì)話變量用于使會(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">
       <h2>Login</h2>
       <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登錄會(huì)話并顯示登錄和注銷鏈接

這是登錄后重定向的目標(biāo)頁面。如果登錄會(huì)話存在,它將顯示注銷鏈接。 一旦超時(shí),它將調(diào)用銷毀會(huì)話。php代碼來銷毀所有會(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 {
       ?>
       <h2>Welcome <?php echo $_SESSION['login-user'];?>!</h2>
       <a href='logout.php'>Log out</a>
<?php
   }
}
?>
</div>
</body>
</html>

此PHP代碼用于希望在會(huì)話到期前注銷的用戶。

它通過要求銷毀會(huì)話來銷毀會(huì)話。php代碼。然后,它將用戶重定向到登錄頁面。 logout.php

<?php
session_start();
require_once __DIR__ . '/destroy-session.php';
header('Location: login.php');
?>

到此,相信大家對“PHP會(huì)話怎么實(shí)現(xiàn)在30分鐘后被銷毀”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI