【问题标题】:How to prevent POST methods from being accessed via GET in Codeigniter?如何防止通过 Codeigniter 中的 GET 访问 POST 方法?
【发布时间】:2013-07-13 04:42:34
【问题描述】:

我正在使用 Codeigniter 。我有以下方法 - create_client 当用户提交填写了所有详细信息的表单时。

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {

public function create_client()
{

// catch all the form data here 
//process form data 


}

}

该函数是为接受表单提交而设计的。但如果有人试图访问 admin/create_client(GET),他也可以直接执行该功能。由于 GET 语句没有表单数据,这会导致错误。

如何防止通过 GET 访问该方法。一种解决方案是在方法中进行一些检查 -

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

// do things here 

} else  {

return false;

}

但是我有很多这样的方法,我不想改变所有这些方法。有没有简单的方法?例如,在 Routes 配置中指定此方法是 POST 函数并且不能通过 GET 访问?

【问题讨论】:

标签: php codeigniter post get routes


【解决方案1】:

试试remap:

public function _remap($method, $params = array())
{
    switch($method) {
        case 'post_method':
        case 'post_method2':
           if (! $_SERVER['REQUEST_METHOD'] === 'POST')
               // return error
    }
    if (method_exists($this, $method))
       return call_user_func_array(array($this, $method), $params);
    show_404();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多