簡體   English   中英

如何在php中設置會話超時?

[英]How to set Session Timeout in php?

我還是 PHP 語言的新手,正在嘗試如何設置會話超時,以確保當用戶登錄他們的帳戶時,當用戶登錄時間過長時,它會在帳戶自動注銷之前限制為幾分鍾/1 小時. 我參考了這個鏈接。

http://bytes.com/topic/php/insights/889606-setting-timeout-php-sessions

索引.php

<?php
if(!isset($_SESSION))
{ 
  session_start(); 
}

$timeout = $_SERVER['REQUEST_TIME'];
/**
 * for a 1 minute timeout, specified in seconds
*/
$timeout_duration = 60;

if (isset($_SESSION['LAST_ACTIVITY']) && ($timeout - $_SESSION['LAST_ACTIVITY']) > $timeout_duration) {
    session_unset();
    session_destroy();
    session_start();
}

$_SESSION['LAST_ACTIVITY'] = $timeout;
?>

優惠券.php

<?php 
// error_reporting(E_ALL); ini_set("display_errors", 1);
session_start();
$timeout = 60; // Number of seconds until it times out.

// Check if the timeout field exists.
if(isset($_SESSION['timeout'])) {
    $duration = time() - (int)$_SESSION['timeout'];
    if($duration > $timeout) {
        // Destroy the session and restart it.
        session_destroy();
    }
}

// Update the timeout field with the current time.
$_SESSION['timeout'] = time();
// include ('sessionTimeout.php');

if( !isset($_SESSION["loginSuccess"]) ){
    echo "<script type='text/javascript'>alert('Login failed!');</script>";
    die('<meta http-equiv="refresh" content="0;URL=\'login-redirect.php\'" />');
}
?>

會話超時.php

<?php
function session_start_timeout($timeout=5, $probability=100, $cookie_domain='/') {
    // Set the max lifetime
    ini_set("session.gc_maxlifetime", $timeout);

    // Set the session cookie to timout
    ini_set("session.cookie_lifetime", $timeout);

    $seperator = strstr(strtoupper(substr(PHP_OS, 0, 3)), "WIN") ? "\\" : "/";
    $path = ini_get("session.save_path") . $seperator . "session_" . $timeout . "sec";
    if(!file_exists($path)) {
        if(!mkdir($path, 600)) {
            trigger_error("Failed to create session save path directory '$path'. Check permissions.", E_USER_ERROR);
        }
    }
    ini_set("session.save_path", $path);

    // Set the chance to trigger the garbage collection.
    ini_set("session.gc_probability", $probability);
    ini_set("session.gc_divisor", 100); // Should always be 100

    // Start the session!
    session_start_timeout(60, 10);

    if(isset($_COOKIE[session_name()])) {
        setcookie(session_name(), $_COOKIE[session_name()], time() + $timeout, $cookie_domain);
    }
}

?>

登出.php

<?php 
session_start();

include('config.php');

foreach($_SESSION as $key => $value){
    if (strpos($key, $PROJECT_NAME) !== FALSE){
        unset($_SESSION[$key]);
    }
}

$_SESSION[$PROJECT_NAME . 'logout'] = true;
session_destroy();

//print_r($_SESSION);
header('Location:' . $base_url . 'index');
?>

我錯過了什么嗎? 這是因為我的會話超時不起作用。

在頁面加載時啟動Javascript計時器,並在timer expires時將用戶redirectlogout頁面。

<script type="text/javascript">
setTimeout(function() { window.location.href = "logout.php"; }, 60 * 60 * 1000);
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM