0
我有一個php網頁,可以在用戶閒置10秒後將用戶註銷。 10秒後,我需要點擊刷新按鈕,然後重定向到主index.php頁面。我如何使一個彈出框顯示「由於不活動而退出」,然後重定向到index.php而不刷新? P/S:我是學習基礎知識的學生,所以我不太瞭解。在會話超時觸發前顯示一條消息
session_start();
$timeout = 10;
// Check if the timeout field exists.
if(isset($_SESSION['timeout'])) {
// See if the number of seconds since the last
// visit is larger than the timeout period.
$duration = time() - (int)$_SESSION['timeout'];
if($duration > $timeout) {
// Destroy the session and restart it.
session_destroy();
session_start();
}
}
So I tried something like this using alert.Why doesn't it work?
<?php
//include ("popup.php");
session_start();
$timeout = 10;
// Check if the timeout field exists.
if(isset($_SESSION['timeout'])) {
// See if the number of seconds since the last
// visit is larger than the timeout period.
$duration = time() - (int)$_SESSION['timeout'];
if($duration > $timeout) {
echo"<script type='javascript'>alert('10 seconds over!');
header("location:../../index.php");
</script>";
}
// Destroy the session and restart it.
session_destroy();
session_start();
header("location:../../index.php");
}
// Update the timout field with the current time.
$_SESSION['timeout'] = time();
你不能用PHP做到這一點,但它不會太容易使用Javascript添加。但是,我可以確認您在轉發到index.php之前不要顯示您的警報嗎?在你的重定向之後做這件事情沒有什麼意義,所以如果有人在不同的網站/類似網站上,他們仍然會看到「已註銷」通知?如果您在重定向之前想要通知,我可以爲您解決。 –
我想要一個類似於「你已經被註銷,因爲不活動」的彈出框,並有一個Ok/some按鈕,當你點擊它後,它會重定向到index.php。 –