Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 通过AJAX请求处理Zend身份验证失败_Php_Zend Framework - Fatal编程技术网

Php 通过AJAX请求处理Zend身份验证失败

Php 通过AJAX请求处理Zend身份验证失败,php,zend-framework,Php,Zend Framework,我目前正在使用Zend控制器插件检查身份验证。以下可能看起来很熟悉: class SF_Plugin_Member_Auth extends Zend_Controller_Plugin_Abstract { public function preDispatch(Zend_Controller_Request_Abstract $request) { if (!SF_Auth::getInstance('Member')->hasIdentity()) {

我目前正在使用Zend控制器插件检查身份验证。以下可能看起来很熟悉:

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());
            }
        }
    }
}
我不确定的是处理未经身份验证的AJAX请求的最佳方法。假设有人试图使用通过AJAX发送的表单登录,Javascript如何知道它实际上需要将用户重定向到登录页面

我的第一个想法是检查该请求是否是AJAX请求,然后回显一个JSON对象,其中包含将用户重定向到何处的详细信息—然后Javascript可以在返回的JSON对象中查找特定属性,并将其用作指向用户的“location.href”的URL

上述问题有两个:

  • 我不知道如何阻止发送请求——如果是AJAX请求,我只想回显一个简单的JSON字符串
  • 这感觉不像是Zend式的做事方式
  • 有没有人偶然发现并解决了这个问题

    非常感谢


    James.

    您可以在响应对象中设置json值,并使用重定向器优雅地停止请求

    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());
           }
        }
    }
    
    这将输出如下内容:

    {"auth":false,"url":"http:\/\/foo.bar\/login"}
    

    通常,如果您正在执行AJAX请求,我假设您正在返回一些标准响应(例如:{error:0,message:“ok”,response:{}}--其中'response'是请求的实际响应对象,'error'和'message'是服务器错误代码和消息)?