温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PHP会话怎么实现在30分钟后被销毁

发布时间:2022-11-15 09:32:52 来源:亿速云 阅读:96 作者:iii 栏目:编程语言

本篇内容主要讲解“PHP会话怎么实现在30分钟后被销毁”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“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(), '', 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会话销毁输出

关于此登录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='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>

仪表板验证PHP登录会话并显示登录和注销链接

这是登录后重定向的目标页面。如果登录会话存在,它将显示注销链接。 一旦超时,它将调用销毁会话。php代码来销毁所有会话。 如果达到30分钟到期时间或会话为空,它会要求用户登录。

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代码用于希望在会话到期前注销的用户。

它通过要求销毁会话来销毁会话。php代码。然后,它将用户重定向到登录页面。 logout.php

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

到此,相信大家对“PHP会话怎么实现在30分钟后被销毁”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI