Jump to content

Using ajax to retain login?


jbonnett

Recommended Posts

Hi All,

I'm trying to use ajax to essentially get a page without changing anything already on the current page, so a modal shows after 29 mins and gives me the option to stop the user from logging out by clicking a button. The php session controls the logout by checking the session timestamp vs the one in the database, if the session one is older than 30 mins with no activity e.g. viewing a page... then logout.

 

The problem is to "view" a page I use an ajax call (although it still logs the user out):

$.ajax({
    cache: false,
    type: "GET",
    dataType: 'html',
    url: "<?=$config["pre_link"].$_SERVER["REQUEST_URI"]?>",
    success: function(data) { },
    error: function(){ },
    complete: function(){ }
}); 
 

Here is the whole thing if you need it:

$(document).ready(function() {
	var timeout = setTimeout(function() {
		$("#logout_warning").modal('show');
	}, 29*60*1000);

	var end = new Date().getTime();

	function resetTimeout() {
		clearTimeout(timeout);
		timeout = setTimeout(function() {
			$("#logout_warning").modal('show');
		}, 29*60*1000);
		end = new Date().getTime();
	}

	$("#stop_logout").click(function() {
		resetTimeout();
		$("#logout_warning").modal('hide');
		$("#logout_warning .modal-content").css("display", "none");
		$.ajax({
			cache: false,
			type: "GET",
			dataType: 'html',
			url: "<?=$config["pre_link"].$_SERVER["REQUEST_URI"]?>",
			success: function(data) { },
			error: function(){ },
    			complete: function(){ }
		});
	});

	$("#stop_logout .close").click(function() {
		$("#logout_warning").modal('hide');
		$("#logout_warning .modal-content").css("display", "none");
	});
					
	var _second = 1000;
	var _minute = _second * 60;
	var _hour = _minute * 60;
	var _day = _hour * 24;
	var timer;
			
	function showRemaining() {
		var now = new Date().getTime();
		var distance = ((end+30*60*1000) - now) - 1000;
		if (distance < 0) {
			clearInterval(timer);
			window.location.reload(true);
			return;
		}
		var days = Math.floor(distance / _day);
		var hours = Math.floor((distance % _day) / _hour);
		var minutes = Math.floor((distance % _hour) / _minute);
		var seconds = Math.floor((distance % _minute) / _second);
			
		//$("#logout_mins").html(minutes);
		$("#logout_secs").html(seconds);
	}
			
	timer = setInterval(showRemaining, 1000);
});
Edited by jbonnett
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.