74 lines
2.3 KiB
PHP
74 lines
2.3 KiB
PHP
<?php
|
|
// Generate a token with 5 minutes expiry
|
|
function generateToken($userId) {
|
|
$expiryTime = time() + (1 * 60); // 5 minutes in seconds
|
|
$tokenPayload = [
|
|
'user_id' => $userId,
|
|
'expiry' => $expiryTime
|
|
];
|
|
|
|
$token = base64_encode(json_encode($tokenPayload)); // Simple token for demonstration
|
|
return $token;
|
|
}
|
|
|
|
$userId = 1; // Assuming user ID is 1
|
|
$token = generateToken($userId);
|
|
?>
|
|
<script>
|
|
// Pass token and expiry time to JavaScript
|
|
var token = '<?php echo $token; ?>';
|
|
</script>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Token Expiry Check</title>
|
|
</head>
|
|
<body>
|
|
|
|
<script>
|
|
function parseToken(token) {
|
|
const tokenParts = token.split('.');
|
|
const payload = JSON.parse(atob(tokenParts[0]));
|
|
return payload;
|
|
}
|
|
|
|
function isTokenExpired(expiry) {
|
|
return Date.now() >= expiry * 1000; // Check if current time is greater than the expiry time
|
|
}
|
|
|
|
function showPopup() {
|
|
alert('Your session has expired! Please log in again.');
|
|
// You can also redirect to login page here if needed
|
|
// window.location.href = '/login.php';
|
|
}
|
|
|
|
const tokenData = parseToken(token); // Parse the token
|
|
|
|
function startWarningInterval() {
|
|
// Start a timer that will show the popup every 2 minutes
|
|
const warningInterval = setInterval(() => {
|
|
showPopup(); // Show the pop-up every 2 minutes
|
|
}, 2 * 60 * 1000); // 2 minutes interval
|
|
|
|
// Clear the warning interval when user clicks "OK" in the alert
|
|
window.addEventListener('click', () => {
|
|
clearInterval(warningInterval); // Stop the interval
|
|
startWarningInterval(); // Restart the interval after the user clicks
|
|
}, { once: true }); // Only fire once after "OK" is clicked
|
|
}
|
|
|
|
// Set an initial interval to check token expiry
|
|
const checkInterval = setInterval(() => {
|
|
if (isTokenExpired(tokenData.expiry)) {
|
|
clearInterval(checkInterval); // Stop checking once token expires
|
|
showPopup(); // Show the pop-up immediately when token expires
|
|
startWarningInterval(); // Start showing the warning every 2 minutes after the token expires
|
|
}
|
|
}, 1000); // Check every second
|
|
</script>
|
|
|
|
</body>
|
|
</html>
|