In my Dashboard.php I call an Ajax Request every 800ms that fetches data from a php file and changes the inner html of an element on my Dashboard.php.
I use it to display a 60 seconds coundown since last activity. Once it reaches 0 it should send the user to logout.php and from there to index.php.
The problem now is that I stay on my page and index.php gets inserted into my div. How can I force it to a site refresh?
Dashboard.php
<script type="text/javascript">
setInterval(function(){
getSessionEnd();
}, 800);
</script>
app.js
var st = document.getElementById("sessiontime");
if (typeof(st) != 'undefined' && st != null){
sessionEnds();
}
function getSessionEnd() {
var xhttp;
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("sessiontime").innerHTML = this.responseText;
}
};
xhttp.open("GET", "./assets/ajax/sessionEnds.php");
xhttp.send();
}
SessionEnds.php
<?php
session_start();
require_once '../../includes/datacon.php';
global $pdo;
$sql = $pdo->prepare('SELECT * FROM userdata JOIN user ON user.id = userdata.uid WHERE username=?');
$sql->bindParam(1, $_SESSION['user']);
$sql->execute();
$sqlu = $sql->fetch( );
$timeS = strtotime($sqlu['lastseen']);
$timeE = strtotime(date("Y-m-d H:i:s", strtotime("+". 1 . " minutes", $timeS)));
$timeD = $timeE - time();
function secondsToTime($seconds) {
$dtF = new DateTime('@0');
$dtT = new DateTime("@$seconds");
return $dtF->diff($dtT)->format('%i minutes and %s seconds');
}
$timeD = secondsToTime($timeD);
if($timeE < time()){
Header('Location: ../../includes/logout.php');
exit();
} else {
echo $timeD;
}
?>
Advertisement
Answer
change to
if($timeE < time()){
echo "0"; // send 0 response
} else {
echo $timeD;// send time
}
also
if (this.readyState == 4 && this.status == 200) {
if (this.responseText=="0" || this.responseText==" 0"){
window.location("logout.php");// got to logout page
}
else
document.getElementById("sessiontime").innerHTML = this.responseText;
}
Change the code as this give the correct path to logout page.. You need to go to the logout page from the current page in browser.