php – 如何在Zend MVC中实现SSL

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 如何在Zend MVC中实现SSL脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我之前使用特定的安全文件夹(例如服务器上的https文件夹vs http文件夹)实现了安全页面.我已经开始使用Zend Framework,并希望部分应用程序(例如登录)使用https.我在GOOGLE搜索过,甚至在这里找不到任何解释如何处理这个问题的内容.我可以为特定控制器/操作设置https吗?谢谢.
最干净的方法是为SSL配置创建一个.ini文件,您可以在其中为模型/控制器/操作级别启用SSL支持,如下所示:

假设您有一个模块/控制器/动作,如下所示:
SSLModule-> IndexController-> testAction


## ini file (can be config.ini also)
ssl.modules.SSLModule.require_ssl = true  //-> entire module requires SSL 
ssl.modules.SSLModule.Index.require_ssl = true  //-> entire controller requires SSL
ssl.modules.SSLModule.Index.test.require_ssl = true  //-> single action requires SSL

你可以通过配置或单独解析它,在你的Bootstrap文件中你可以包含一个controllerplugin,就像我的一样.

还有很多其他方法可以做到一点,但我认为你明白了!


class Application_Controllerplugins_Ssl extends Zend_Controller_Plugin_Abstract
{

    public function predispatch ( Zend_Controller_Request_Abstract $request )
    {

        $shouldSecureUrl = false;

        //get the config settings for SSL
        $options = Application_ServiceManager::getConfig()->ssl;

        //if config is empty,exIT
        if (!is_object($options))
            return;

        //simpler to use    
        $options = $options->toArray();

        //only use it PRoduction environment
        if ( APPLICATION_ENV == 'production' )
        {

            if (

                ( isset($options['modules'][$request->;module]['require_ssl']) && $options['modules'][$request->module]['require_ssl'] )  ||
                ( isset($options['modules'][$request->module][$request->controller]['require_ssl']) && $options['modules'][$request->module][$request->controller]['require_ssl'] )  ||
                ( isset($options['modules'][$request->module][$request->controller][$request->action]['require_ssl']) && $options['modules'][$request->module][$request->controller][$request->action]['require_ssl'] )

            )
            {

                $shouldSecureUrl = true;

            }

            if ( $shouldSecureUrl )
            {

                $this->_secureUrl($request);

            }
        }
    }


    protected function _secureUrl ( Zend_Controller_Request_Abstract $request )
    {

        $server = $request->getServer();
        $hostname = $server['HTTP_HOST'];

        if ( ! $request->isSecure() )
        {
            $url = Zend_Controller_Request_Http::SCHEME_HTTPS . "://" . $hostname .
             $request->getPathInfo();

            $redirector = Zend_Controller_Action_HelPErbroker::getstaticHelper('redirector');
            $redirector->setGoToUrl($url);
            $redirector->redirectAndExit();
        }
    }
}

我忘了提到:将它添加到你的引导程序中:


$Zend_Controller_Front->registerPlugin( new Application_Controllerplugins_Ssl() );

脚本宝典总结

以上是脚本宝典为你收集整理的php – 如何在Zend MVC中实现SSL全部内容,希望文章能够帮你解决php – 如何在Zend MVC中实现SSL所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。