0

Is there a way I can have a plugin redirect the whole page when it is getting called through an AJAX request?

Here is the code I am using to redirect non-authenticated users to the login page.

public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) {
    if ($request->getControllerName() != 'authentication') {
        if (!Zend_Auth::getInstance()->hasIdentity()) {
            $request->setControllerName('authentication');
            $request->setActionName('login');
            return;
        }
    }
}

This works for normal requests but I want it to redirect users to the login page when they make AJAX requests after they are logged out. Using what I have now it loads the HTML for the login page inside the HTML element that is designated to house the result of the AJAX request.

How can I have it redirect the browser to the login page instead of just loading the login page inside the current page?

bconrad
  • 402
  • 4
  • 14
  • The client side JavaScript that makes the Ajax call will have to examine the response and decide if it needs to redirect. An Ajax call cannot force the calling page to redirect. In the plugin, check to see if the call is made with Ajax and if so, output a special response so the calling code can redirect. – drew010 Jun 06 '12 at 18:05
  • Ok. I wanted to see if I could do that without using client side script to handle redirecting. This works, though. – bconrad Jun 07 '12 at 19:57

1 Answers1

0

Well if i'm understanding you correctly. drew010 is correct, the ajax call is a different http request not the one the user is actually seeing and interacting with. So without using javascript php can't leap from one http connection to a different connection and execute a redirect. PHP isn't an 'active' language it's a 'reactive' language. meaning it doesn't do anything until the page is loading/refreshing.

But if you want to handle the redirect via ajax or not ajax it's your initiator to the redirect. since that's whats actually making the call to trigger the whole process your wanting anyway.

Matopicus
  • 131
  • 1