PHP会话怎么实现在30分钟后被销毁-成都快上网建站

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

// 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

session_start();
$expirtyMinutes = 1;
?>


PHP Session Destroy after 30 Minutes




   

       

Login


       

           
               
                   
                   
               
               
                   
                   
               
               
                   
               
           
Username
Password
                       name="submit">

       

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!";
   }
}
?>



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

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

home.php

session_start();
?>


PHP Session Destroy after 30 Minutes




   

if (! isset($_SESSION['login-user'])) {
   echo "Login again!

";
   echo "Login";
} else {
   $currentTime = time();
   if ($currentTime > $_SESSION['expiry-time']) {
       require_once __DIR__ . '/destroy-session.php';
       echo "Session expired!

Login";
   } else {
       ?>
       

Welcome !


       Log out
   }
}
?>


此PHP代码用于希望在会话到期前注销的用户。

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

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

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


分享名称:PHP会话怎么实现在30分钟后被销毁
文章地址:http://kswjz.com/article/ijpoch.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流