本文給大家介紹有關(guān)php會(huì)話如何指定時(shí)間銷毀的問題,下面就給大家詳細(xì)介紹如何通過session_destroy()這個(gè)函數(shù)來銷毀會(huì)話的,希望對(duì)需要的朋友有所幫助~
PHP有一個(gè)核心函數(shù)session_destroy()來清除所有會(huì)話值。它是一個(gè)簡(jiǎn)單的沒有參數(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è)置。
立即學(xué)習(xí)“PHP免費(fèi)學(xué)習(xí)筆記(深入)”;
這個(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ì)話變量。對(duì)于較舊的PHP版本,請(qǐng)使用session_unset()。 php會(huì)話銷毀輸出【推薦學(xué)習(xí):PHP視頻教程】
讓我們創(chuàng)建一個(gè)登錄示例代碼,以使用PHP會(huì)話、session_destroy等。它允許用戶從當(dāng)前會(huì)話登錄和注銷。如果您在PHP腳本中尋找完整的用戶注冊(cè)和登錄,請(qǐng)使用此代碼。 此示例提供了自動(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"> <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>
這是登錄后重定向的目標(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 { ?> <h1>Welcome <?php echo $_SESSION['login-user'];?>!</h1> <a href='logout.php'>Log out</a> <?php } } ?> </div> </body> </html>
它通過要求銷毀會(huì)話來銷毀會(huì)話。php代碼。然后,它將用戶重定向到登錄頁面。 logout.php
<?php session_start(); require_once __DIR__ . '/destroy-session.php'; header('Location: login.php'); ?>
我希望這個(gè)示例有助于理解如何銷毀PHP會(huì)話。而且,這是一個(gè)完美的場(chǎng)景,適合解釋銷毀會(huì)話的必要性。
本文系轉(zhuǎn)載,原文地址:https://juejin.cn/post/7164391542164520990
以上就是詳解PHP會(huì)話如何實(shí)現(xiàn)在30分鐘后被銷毀(附代碼實(shí)例)的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!
PHP怎么學(xué)習(xí)?PHP怎么入門?PHP在哪學(xué)?PHP怎么學(xué)才快?不用擔(dān)心,這里為大家提供了PHP速學(xué)教程(入門到精通),有需要的小伙伴保存下載就能學(xué)習(xí)啦!
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://www.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)