我有這個函數來跟蹤用戶會話時間。關於session_start()的致命錯誤
function sessionTracker(){
ini_set('session.cookie_lifetime', 86400);
ini_set('session.gc_maxlifetime', 86400);
$sid = session_id();
if ($sid != '') {
//echo "Session exists!";
} else {
//echo "No current session exists! Starting...";
session_start();
}
if(!isset($_SESSION['username']))
{
header("location:login.php");
exit();
}
但我發現了一個「30秒超過了最大的執行時間」,當它擊中的session_start();線。有沒有更好的功能,或者我該如何解決這個問題?我一直都不明白,但總是那樣。
function sessionReup() {
$time = $_SERVER['REQUEST_TIME'];
//for a 30 minute timeout, specified in seconds
$timeout_duration = 1800;
/*
Here we look for the user’s LAST_ACTIVITY timestamp. If
it’s set and indicates our $timeout_duration has passed,
blow away any previous $_SESSION data and start a new one.
*/
if (isset($_SESSION['LAST_ACTIVITY']) && ($time - $_SESSION['LAST_ACTIVITY']) > $timeout_duration) {
session_unset();
session_destroy();
session_start();
header("location:login.php");
exit();
}
/*
Finally, update LAST_ACTIVITY so that our timeout
is based on it and not the user’s login time.
*/
$_SESSION['LAST_ACTIVITY'] = $time;
}
嘗試增加的max_execution_time在php.ini –
但如果增加時間,不增加時間的頁面將加載?那麼用戶會等待更長的時間? – moe
是的,但如果頁面需要加載時間,那麼我們應該給它。並非所有頁面都需要很長的執行時間 –