微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

本篇内容主要讲解“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分钟后被销毁”有了更深的了解,不妨来实际操作一番吧!这里是编程之家网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐