我正在使用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访问?
尝试重新映射:
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();
}