如何在1分钟内破坏会话?

时间:2017-07-08 05:14:41

标签: php

我最近尝试但是我没能在1分钟内破坏会话。我正在使用下面给出的PHP销毁代码,但它无法正常工作。

<?php 
    session_start();
    $_SESSION['start'] = "start session";
    if(isset($_POST['destroy'])){
        session_cache_expire(1);
    } 
?>    

2 个答案:

答案 0 :(得分:0)

session_start();

// 1 mins in seconds
$inactive = 60; 

$session_life = time() - $_session['timeout'];

if($session_life > $inactive)
{  session_destroy(); header("Location: logoutpage.php");     }

S_session['timeout']=time();

参考此link

或者您可以使用

ini_set('session.gc_maxlifetime',1);

答案 1 :(得分:0)

        <?php
        define("DURATION",'1'); // define duration constant in minutes
        session_start();
        $duration = (DURATION * 60); // duration in seconds

        if(isset($_SESSION['started'])) //check whether session is set or not
        {
            $time = ($duration - (time() - $_SESSION['started']));
            if($time <= 0)
            {
                session_destroy();
                echo "Session has been expired. You were logged in for one minute";
            }
        } 
       else
        {
            $_SESSION['started'] = time();
        }
        ?>