【问题标题】:https request on some pages but not on all pages zend frameworkhttps 请求在某些页面上,但不是在所有页面上 zend 框架
【发布时间】:2010-09-23 10:33:18
【问题描述】:

我需要将 https 放在某些 URL 上,但不是放在所有 URL 上。我正在为所有链接使用 zend URl 视图助手。我有整个站点的 *.example.com SSL 证书。现在我用https://www.example.co打开网站,然后主页或其他页面上的所有链接都在URL中包含https。如何在https url上提出一些特定的请求,其他页面应该可以正常打开。

我还需要进行一些重定向,这样如果有人在正常 URL 中打开特定页面,他们就会重定向到 https url。我认为 .htaccess 重定向可以解决这个问题。

任何帮助????

提前致谢!!!

【问题讨论】:

  • 好问题。如果请求是HTTPS,您能否检查该操作,如果不是,则重定向到同一操作的HTTPS URL?

标签: php zend-framework


【解决方案1】:

URL ViewHelper 只组装路径,绝对来自主机名。所以你需要明确地为你的 https 链接添加前缀

<? $url = $view->url(array('some' => 'params') /*, $route, $reset*/) ?>
<a href="https://<?= $_SERVER['HTTP_HOST] ?><?= $url ?>">my explicit https link</a>

您也许应该创建一个自己的小型 viewhelper 来为您工作,并检查是否设置了 HTTP_HOST 等,也可以从配置中获取它,而不是从 $_SERVER 获取。

$view->httpsUrl(array('some' => 'params')/, $route, $reset/);

为了确保定义的请求必须是 https 的安全,可以通过添加前端控制器插件甚至是一个抽象的控制器类来轻松完成,您将所有 otehr 控制器都基于该类。

插件可能看起来像这样

My_Controller_Plugin_HttpBlacklist extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        // when /foo/bar/baz is requested
        if (($request->getModuleName() == 'foo' &&
            $request->getControllerName() == 'bar' &&
            $request->getControllerName() == 'baz')
            /* || (conditions for more requests)*/) {

            //very basic confifiotn to see if https is enabled, should be done better...
            if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on') {

                // should be done with Zend_Http_Reponse instead
                header('Location: https://'. $_SERVER['HTTP_HOST] . $_SERVER['REQUEST_URI']);
                exit;
            }
        }
    }
}

然后直接插入

$frontController->registerPlugin(new My_Controller_Plugin_HttpBlacklist);

【讨论】:

    【解决方案2】:

    我宁愿使用一种简单的方法,并且不需要对 url 生成/路由进行任何更改。我在插件的 routeStartup 函数中写了以下几行,并且没有对 URL 路由进行任何更改。

    public function routeStartup(Zend_Controller_Request_Abstract $request)
    {
    
       $controller=$this->_controller = $this->_front->getRequest()->getControllerName();
       $action=$this->_controller = $this->_front->getRequest()->getActionName();
    
        if($_SERVER['SERVER_PORT']!=443)
        {
    //controller and actions array to check ssl links
            $actionArr=array('index'=>array('registration'),'account'=>array('editacc','edit'),'dealer'=>array('*'));
            if(array_key_exists($controller,$actionArr)!==false)
            {
                if(in_array($action,$actionArr[$controller]) || $actionArr[$controller][0]=='*')
                {
                    $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
                     $redirector->gotoUrl(SITE_LIVE_URL_SSL.$controller."/".$action);    
                }
            }
    
    
        }
        else
        {
    //controller and action array that should not be on ssl.
              $notAactionArr=array('usersearch'=>array('mainserarch'),'account'=>array('index'),'onlineusers'=>array('*'));
            if(array_key_exists($controller,$notAactionArr)!==false)
            {
                if(in_array($action,$notAactionArr[$controller]) || $notAactionArr[$controller][0]=='*')
                {
                    $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
                     $redirector->gotoUrl(SITE_LIVE_URL.$controller."/".$action);    
                }
            }
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-05
      • 2020-03-17
      • 2014-09-16
      • 1970-01-01
      • 1970-01-01
      • 2015-10-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多