I am working with api in php/ajax and i have in every page included test if a user hasnt been logged out because of inactivity after a certain time. It works but in some moments i have troubles. Here’s the story:
I have one file called index.php. Index.php has a div – let’s name it ‘div’. It also has some buttons – let’s name it “new” and “modify”. When i press a button, body of file new.php and modify.php is loaded via ajax method to ‘div’. So far so good. But when user becomes inactive for certain time it should logout him. And it does. But unfortunatelly – and it’s clear why it is happening – logout page is not printed into index.php, but to the body of “div”. It’s easy to understand – pressing “new” button moves me to new.php. New.php checks if i am logged in – i am not so it redirects me to logout.page so new.php is resulting logout page and it becomes a body of my “div”. Here’s the question: how to redirect whole page (index.php) to logout page if i am logged out and not print it in div.
Remember that pressing “new” button doesnt refresh index.php becuase i use ajax. It only refreshes my div. Is it even possible?
Don’t do your redirect in
new.php, instead make that PHP script return some particular value when the user is logged out, analyze that output in your AJAX call and do the redirect via javascript if necessary.In other words:
new.phpsucceeds do what you normally donew.phpreturns some “authentication failure” status, do your redirectThe best way is to encapsulate AJAX responses in a JSON object so you can have both data and response codes for easier analizing on the javascript side.