1

我正在使用 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 访问?

4

1 回答 1

0

尝试重新映射

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();
}
于 2013-07-13T05:00:31.643 回答