【问题标题】:Prevent direct access to an action controller防止直接访问动作控制器
【发布时间】:2012-02-16 10:55:14
【问题描述】:

我正在使用 zend 框架。在一个视图中,我使用了一个动作助手。

echo $this->action('info', 'comment', null, array('articleID' => $article->arti_id));

因此,助手调用“CommentController”类和方法“infoAction”。在我的方法中,我得到了“articleID”参数,并开始使用模型进行处理。最后我渲染了一个共享视图。

public function infoAction() {
    $articleID = $this->getRequest()->getParam('articleID');
    // Working with model
            // .....
    $this->renderScript('/View/Script/DefaultComment.phtml');
}

它工作得很好,但我不希望这个控制器/动作可以通过 url domain.com/comment/info 直接访问。

我怎样才能避免这种情况?

谢谢

【问题讨论】:

    标签: php zend-framework


    【解决方案1】:

    如果您的操作方法总是使用文章 id 参数调用,那么您可以在没有 id 的情况下重定向请求:

    public function infoAction() {
        if ($this->_hasParam('articleID')) {
            $articleID = $this->getRequest()->getParam('articleID');
            // Working with model
                // .....
        $this->renderScript('/View/Script/DefaultComment.phtml');
        } else {
            $this->_helper->redirector('index', 'index', 'default');
        }
    }
    

    【讨论】:

    • 是的,但是由于您的 URL 在您的代码源中可见,因此直接访问此页面不应构成安全漏洞。但是可能会出现代理页面...您必须知道 action() 非常昂贵,并且 View Helper 更适合实现这种操作。
    • 最后,我创建了一个视图助手并渲染了我的视图。正如您所说,控制器必须保持公开/可访问。我的助手受到更多保护。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-14
    相关资源
    最近更新 更多