国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

目錄
快速範(fàn)例
關(guān)於此登入session_destory()範(fàn)例
帶有登入表單的登入頁面
儀表板驗證PHP登入工作階段並顯示登入和登出連結(jié)
此PHP程式碼用於希望在會話到期前登出的使用者。
首頁 後端開發(fā) php教程 詳解PHP會話如何實現(xiàn)在30分鐘後被銷毀(附程式碼實例)

詳解PHP會話如何實現(xiàn)在30分鐘後被銷毀(附程式碼實例)

Nov 14, 2022 pm 04:34 PM
php 會話

本文要為大家介紹PHP會話如何指定時間銷毀的問題,以下就給大家詳細介紹如何透過session_destroy()這個函數(shù)來銷毀會話的,希望對需要的朋友有所幫助~

詳解PHP會話如何實現(xiàn)在30分鐘後被銷毀(附程式碼實例)

#PHP有一個核心函數(shù)session_destroy()來清除所有會話值。它是一個簡單的沒有參數(shù)的函數(shù),傳回一個布林值true或false。

PHP的會話ID預(yù)設(shè)儲存在一個cookie中。一般來說,該會話cookie檔案的名字是PHPSESSID。 session_destroy函數(shù)不會取消cookie中的sessionid。

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

這個快速的範(fàn)例使用session_destroy()來銷毀會話。它使用set_cookie()方法,透過過期的PHP會話ID來殺死整個會話。

快速範(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(), &#39;&#39;, time() - 42000, $cookieParam["path"], $cookieParam["domain"], $cookieParam["secure"], $cookieParam["httponly"]);
}

// as a last step, destroy the session.
session_destroy();

註: 使用session_start()在PHP會話銷毀後重新啟動會話。 使用PHP$_SESSION取消設(shè)定特定的會話變數(shù)。對於較舊的PHP版本,請使用session_unset()。 php會話銷毀輸出【推薦學(xué)習(xí):PHP影片教學(xué)

關(guān)於此登入session_destory()範(fàn)例

讓我們建立一個登入範(fàn)例程式碼,以使用PHP會話、 session_destroy等。它允許用戶從當(dāng)前會話登入和登出。如果您在PHP腳本中尋找完整的使用者註冊和登錄,請使用此程式碼。 此範(fàn)例提供了自動登入會話到期功能。

帶有登入表單的登入頁面

此表單發(fā)佈使用者輸入的使用者名稱和密碼。它驗證PHP中的登入憑證。 成功登入後,它將登入狀態(tài)儲存到PHP會話。它將過期時間設(shè)定為從上次登入時間起30分鐘。 它將上次登入時間和到期時間儲存到PHP會話。這兩個會話變數(shù)用於使會話自動過期。

login.php

<?php
session_start();
$expirtyMinutes = 1;
?>
<html>
<head>
<title>PHP Session Destroy after 30 Minutes</title>
<link rel=&#39;stylesheet&#39; href=&#39;style.css&#39; type=&#39;text/css&#39; />
<link rel=&#39;stylesheet&#39; href=&#39;form.css&#39; type=&#39;text/css&#39; />
</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[&#39;submit&#39;])) {
    $usernameRef = "admin";
    $passwordRef = "test";
    $username = $_POST[&#39;username&#39;];
    $password = $_POST[&#39;password&#39;];

    // 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[&#39;login-user&#39;] = $username;
        // login time is stored as reference
        $_SESSION[&#39;ref-time&#39;] = 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 &#39;last active time&#39;.
        // 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[&#39;expiry-time&#39;] = time() + ($expirtyMinutes * 60);
        // redirect to home
        // do not include home page, it should be a redirect
        header(&#39;Location: home.php&#39;);
    } else {
        echo "Wrong username or password. Try again!";
    }
}
?>
</div>
</body>
</html>

儀表板驗證PHP登入工作階段並顯示登入和登出連結(jié)

這是登入後重新導(dǎo)向的目標(biāo)頁面。如果登入會話存在,它將顯示註銷連結(jié)。 一旦超時,它將呼叫銷毀會話。 php程式碼來銷毀所有會話。 如果達到30分鐘到期時間或會話為空,它會要求使用者登入。

home.php

<?php
session_start();
?>
<html>
<head>
<title>PHP Session Destroy after 30 Minutes</title>
<link rel=&#39;stylesheet&#39; href=&#39;style.css&#39; type=&#39;text/css&#39; />
<link rel=&#39;stylesheet&#39; href=&#39;form.css&#39; type=&#39;text/css&#39; />
</head>
<body>
    <div class="phppot-container">
<?php
if (! isset($_SESSION[&#39;login-user&#39;])) {
    echo "Login again!<br><br>";
    echo "<a href=&#39;login.php&#39;>Login</a>";
} else {
    $currentTime = time();
    if ($currentTime > $_SESSION[&#39;expiry-time&#39;]) {
        require_once __DIR__ . &#39;/destroy-session.php&#39;;
        echo "Session expired!<br><br><a href=&#39;login.php&#39;>Login</a>";
    } else {
        ?>
        <h1>Welcome <?php echo $_SESSION[&#39;login-user&#39;];?>!</h1>
        <a href=&#39;logout.php&#39;>Log out</a>
<?php
    }
}
?>
</div>
</body>
</html>

此PHP程式碼用於希望在會話到期前登出的使用者。

它透過要求銷毀會話來銷毀會話。 php代碼。然後,它將使用者重新導(dǎo)向到登入頁面。 logout.php

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

我希望這個範(fàn)例有助於理解如何銷毀PHP會話。而且,這是一個完美的場景,適合解釋銷毀會話的必要性。

本文系轉(zhuǎn)載,原文網(wǎng)址:https://juejin.cn/post/7164391542164520990

以上是詳解PHP會話如何實現(xiàn)在30分鐘後被銷毀(附程式碼實例)的詳細內(nèi)容。更多資訊請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

我如何了解最新的PHP開發(fā)和最佳實踐? 我如何了解最新的PHP開發(fā)和最佳實踐? Jun 23, 2025 am 12:56 AM

TostaycurrentwithPHPdevelopmentsandbestpractices,followkeynewssourceslikePHP.netandPHPWeekly,engagewithcommunitiesonforumsandconferences,keeptoolingupdatedandgraduallyadoptnewfeatures,andreadorcontributetoopensourceprojects.First,followreliablesource

什麼是PHP,為什麼它用於Web開發(fā)? 什麼是PHP,為什麼它用於Web開發(fā)? Jun 23, 2025 am 12:55 AM

PHPbecamepopularforwebdevelopmentduetoitseaseoflearning,seamlessintegrationwithHTML,widespreadhostingsupport,andalargeecosystemincludingframeworkslikeLaravelandCMSplatformslikeWordPress.Itexcelsinhandlingformsubmissions,managingusersessions,interacti

如何設(shè)置PHP時區(qū)? 如何設(shè)置PHP時區(qū)? Jun 25, 2025 am 01:00 AM

tosetTherightTimeZoneInphp,restate_default_timezone_set()functionAtthestArtofyourscriptWithavalIdidentIdentifiersuchas'america/new_york'.1.usedate_default_default_timezone_set_set()

我如何驗證PHP中的用戶輸入以確保其符合某些標(biāo)準? 我如何驗證PHP中的用戶輸入以確保其符合某些標(biāo)準? Jun 22, 2025 am 01:00 AM

TovalidateuserinputinPHP,usebuilt-invalidationfunctionslikefilter_var()andfilter_input(),applyregularexpressionsforcustomformatssuchasusernamesorphonenumbers,checkdatatypesfornumericvalueslikeageorprice,setlengthlimitsandtrimwhitespacetopreventlayout

什麼是php(serialize(),Unserialize())中的數(shù)據(jù)序列化? 什麼是php(serialize(),Unserialize())中的數(shù)據(jù)序列化? Jun 22, 2025 am 01:03 AM

thephpfunctionserize()andunSerialize()redustoconvertComplexdatStructDestoresToroStoroStoroSandaBackagagain.1.Serialize()

如何將PHP代碼嵌入HTML文件中? 如何將PHP代碼嵌入HTML文件中? Jun 22, 2025 am 01:00 AM

可以將PHP代碼嵌入HTML文件中,但需確保文件以.php為擴展名,以便服務(wù)器能正確解析。使用標(biāo)準的標(biāo)籤包裹PHP代碼,可在HTML中任意位置插入動態(tài)內(nèi)容。此外,可在同一文件中多次切換PHP與HTML,實現(xiàn)條件渲染等動態(tài)功能。務(wù)必注意服務(wù)器配置及語法正確性,避免因短標(biāo)籤、引號錯誤或遺漏結(jié)束標(biāo)籤導(dǎo)致問題。

編寫清潔和可維護的PHP代碼的最佳實踐是什麼? 編寫清潔和可維護的PHP代碼的最佳實踐是什麼? Jun 24, 2025 am 12:53 AM

寫乾淨(jìng)、易維護的PHP代碼關(guān)鍵在於清晰命名、遵循標(biāo)準、合理結(jié)構(gòu)、善用註釋和可測試性。 1.使用明確的變量、函數(shù)和類名,如$userData和calculateTotalPrice();2.遵循PSR-12標(biāo)準統(tǒng)一代碼風(fēng)格;3.按職責(zé)拆分代碼結(jié)構(gòu),使用MVC或Laravel式目錄組織;4.避免麵條式代碼,將邏輯拆分為單一職責(zé)的小函數(shù);5.在關(guān)鍵處添加註釋並撰寫接口文檔,明確參數(shù)、返回值和異常;6.提高可測試性,採用依賴注入、減少全局狀態(tài)和靜態(tài)方法。這些做法提升代碼質(zhì)量、協(xié)作效率和後期維護便利性。

如何使用PHP執(zhí)行SQL查詢? 如何使用PHP執(zhí)行SQL查詢? Jun 24, 2025 am 12:54 AM

Yes,youcanrunSQLqueriesusingPHP,andtheprocessinvolveschoosingadatabaseextension,connectingtothedatabase,executingqueriessafely,andclosingconnectionswhendone.Todothis,firstchoosebetweenMySQLiorPDO,withPDObeingmoreflexibleduetosupportingmultipledatabas

See all articles