Link to home
Start Free TrialLog in
Avatar of Marco Gasi
Marco GasiFlag for Spain

asked on

redirect to login page

Hi everybody.
I have a web application where the user must be logged in: no problem here. In each page I check if the user is logged and if it is not he is redirected to the login page.
But within the application, many operations involve the use of Ajax calls to several php scripts. The problem is this: if the user keep the page opened enough without doing anything, the session expires so the user is no more logged in, but if he come back to the app and click a button to do something the page continue loading without do anything.

My solution has been to put the check even in php scripts:
if ( !isset( $_SESSION[ 'user_id' ] ) )
{
	echo json_encode('expired');
}

Open in new window

and then, in javascript:
var response = JSON.parse(data);
if ( response === 'expired' ) {
	window.location.href = globalURL;
	return;
}

Open in new window

but this has no effect: the page remains with loading spinner and stays there.
What am I doing wrong?
ASKER CERTIFIED SOLUTION
Avatar of Mukesh Yadav
Mukesh Yadav
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Marco Gasi

ASKER

You're right, thank you!