登录  /  注册

详解PHP会话如何实现在30分钟后被销毁(附代码实例)

藏色散人
发布: 2022-11-14 16:34:51
转载
1824人浏览过

本文给大家介绍有关php会话如何指定时间销毁的问题,下面就给大家详细介绍如何通过session_destroy()这个函数来销毁会话的,希望对需要的朋友有所帮助~

详解PHP会话如何实现在30分钟后被销毁(附代码实例)

PHP有一个核心函数session_destroy()来清除所有会话值。它是一个简单的没有参数的函数,返回一个布尔值true或false。

PHP的会话ID默认存储在一个cookie中。一般来说,该会话cookie文件的名字是PHPSESSID。session_destroy函数不会取消cookie中的sessionid。

为了 "完全 "销毁会话,会话ID也必须被取消设置。

这个快速的例子使用session_destroy()来销毁会话。它使用set_cookie()方法,通过过期的PHP会话ID来杀死整个会话。

快速例子

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取消设置特定的会话变量。对于较旧的PHP版本,请使用session_unset()。 php会话销毁输出【推荐学习:PHP视频教程

关于此登录session_destory()示例

让我们创建一个登录示例代码,以使用PHP会话、session_destroy等。它允许用户从当前会话登录和注销。如果您在PHP脚本中寻找完整的用户注册和登录,请使用此代码。 此示例提供了自动登录会话到期功能。

带有登录表单的登录页

此表单发布用户输入的用户名和密码。它验证PHP中的登录凭据。 成功登录后,它将登录状态存储到PHP会话中。它将过期时间设置为从上次登录时间起30分钟。 它将上次登录时间和到期时间存储到PHP会话中。这两个会话变量用于使会话自动过期。

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登录会话并显示登录和注销链接

这是登录后重定向的目标页面。如果登录会话存在,它将显示注销链接。 一旦超时,它将调用销毁会话。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代码。然后,它将用户重定向到登录页面。 logout.php

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

我希望这个示例有助于理解如何销毁PHP会话。而且,这是一个完美的场景,适合解释销毁会话的必要性。

本文系转载,原文地址:https://juejin.cn/post/7164391542164520990

以上就是详解PHP会话如何实现在30分钟后被销毁(附代码实例)的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:juejin网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号