AJAX not redirecting if user unloads the page

Question!

I'm trying to redirect a user if they have logged in to my site and left the page. I'm not having a problem logging them out or closing the session, however, if they hit the back button on their browser they're able to see the template for the profile page but none of the information associated with the user that was logged in. I don't want this, I want the user to be redirected to the login page if they hit the browser back button after having navigated away from their profile while logged in. Is there a way to do this? If so, how?

This script calls the logout.php page and loggs them out if the page has been unloaded and then checks to see if the page has been reloaded and should redirect them to the logged_out page. This is what's not working.

$(window).unload(function(){
    $.post("../php/logout.php", {}, function(response){
        if(response.success == "1"){
            if(location.reload(true)){
                location.replace("../pages/logged_out.php");
            }
        }
    }, "json");
})


Answers

If the issue is redirecting the user after they hit the back button, you should add a check at the top of the Profile/Template page to redirect the user to the login page.

In general practice, this is the very first thing I do at the top of every page (in a function call) so I can make sure no one is hitting a page they shouldn't be if they aren't logged in.

By : Mark


This video can help you solving your question :)
By: admin