Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 979027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:09:15+00:00 2026-05-16T04:09:15+00:00

I’m currently using a Zend Controller Plugin to check authentication. The following probably looks

  • 0

I’m currently using a Zend Controller Plugin to check authentication. The following probably looks familiar:

class SF_Plugin_Member_Auth extends Zend_Controller_Plugin_Abstract {

    public function preDispatch(Zend_Controller_Request_Abstract $request) {

        if (!SF_Auth::getInstance('Member')->hasIdentity()) {
            if ($request->getControllerName() !== 'auth' && $request->getControllerName() !== 'error') {
                $r = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
                $r->gotoSimpleAndExit('login', 'auth', $request->getModuleName());
            }
        }
    }
}

What I’m unsure of is the best way of dealing with an AJAX request that isn’t authenticated. So say someone tries to login using a form that’s sent over AJAX, how should the Javascript know that it actually needs to redirect the user to the login page?

My first thought is to check to see if the request is an AJAX request, and then echo out a JSON object with details of where to redirect the user to – the Javascript can then look for a particular property in the returned JSON object and use that as the URL to “location.href” the user to.

There are two problems with the above:

  1. I’m not sure how to stop the request from being dispatched – all I want to do is echo out a simple JSON string if it’s an AJAX request.
  2. It doesn’t feel like a Zend-like way of doing things.

Is there anyone out there who’s hit upon and solved this very scenario?

Thanks very much,

James.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:09:15+00:00Added an answer on May 16, 2026 at 4:09 am

    You can set your json values in the response object and gracefully stop the request with the redirector.

    if (!SF_Auth::getInstance('Member')->hasIdentity()) {
        if ($request->getControllerName() !== 'auth' && $request->getControllerName() !== 'error') {
            if ($request->isXmlHttpRequest()) {
                $json = Zend_Json::encode(array('auth' => false, 'url' => 'http://foo.bar/login'));
    
                // Prepare response
                $this->getResponse()
                     ->setHttpResponseCode(200) // Or maybe HTTP Status 401 Unauthorized
                     ->setBody($json)
                     ->sendResponse();
    
                // redirectAndExit() cleans up, sends the headers and stopts the script
                Zend_Controller_Action_HelperBroker::getStaticHelper('redirector')->redirectAndExit();
            } else {        
                $r = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
                $r->gotoSimpleAndExit('login', 'auth', $request->getModuleName());
           }
        }
    }
    

    This will output something like this:

    {"auth":false,"url":"http:\/\/foo.bar\/login"}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.