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

詳解PHP會(huì)話如何實(shí)現(xiàn)在30分鐘后被銷毀(附代碼實(shí)例)

藏色散人
發(fā)布: 2022-11-14 16:34:51
轉(zhuǎn)載
2242人瀏覽過

本文給大家介紹有關(guān)php會(huì)話如何指定時(shí)間銷毀的問題,下面就給大家詳細(xì)介紹如何通過session_destroy()這個(gè)函數(shù)來銷毀會(huì)話的,希望對(duì)需要的朋友有所幫助~

詳解PHP會(huì)話如何實(shí)現(xiàn)在30分鐘后被銷毀(附代碼實(shí)例)

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();
登錄后復(fù)制

注: 使用session_start()在PHP會(huì)話銷毀后重新啟動(dòng)會(huì)話。 使用PHP$_SESSION取消設(shè)置特定的會(huì)話變量。對(duì)于較舊的PHP版本,請(qǐng)使用session_unset()。 php會(huì)話銷毀輸出【推薦學(xué)習(xí):PHP視頻教程

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

讓我們創(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>
登錄后復(fù)制

儀表板驗(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 {
        ?>
        <h1>Welcome <?php echo $_SESSION['login-user'];?>!</h1>
        <a href='logout.php'>Log out</a>
<?php
    }
}
?>
</div>
</body>
</html>
登錄后復(fù)制

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

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

<?php
session_start();
require_once __DIR__ . '/destroy-session.php';
header('Location: login.php');
?>
登錄后復(fù)制

我希望這個(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é)教程(入門到精通)
PHP速學(xué)教程(入門到精通)

PHP怎么學(xué)習(xí)?PHP怎么入門?PHP在哪學(xué)?PHP怎么學(xué)才快?不用擔(dān)心,這里為大家提供了PHP速學(xué)教程(入門到精通),有需要的小伙伴保存下載就能學(xué)習(xí)啦!

下載
相關(guān)標(biāo)簽:
來源:juejin網(wǎng)
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請(qǐng)聯(lián)系admin@php.cn
最新問題
開源免費(fèi)商場(chǎng)系統(tǒng)廣告
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長(zhǎng)!
關(guān)注服務(wù)號(hào) 技術(shù)交流群
PHP中文網(wǎng)訂閱號(hào)
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號(hào)
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://www.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)