在我的一个控制器中,我使用了 codeigniter 的表单验证助手。我的验证规则之一是我创建的返回 true 或 false 的自定义函数。问题是,这个验证函数是在控制器的类中声明的。如何防止用户浏览我的验证功能作为控制器操作(mysite/mycontroller/my_callback_func)?
PS - 试图将函数设置为私有,但我收到一个错误,验证助手无法访问它。
在我的一个控制器中,我使用了 codeigniter 的表单验证助手。我的验证规则之一是我创建的返回 true 或 false 的自定义函数。问题是,这个验证函数是在控制器的类中声明的。如何防止用户浏览我的验证功能作为控制器操作(mysite/mycontroller/my_callback_func)?
PS - 试图将函数设置为私有,但我收到一个错误,验证助手无法访问它。
只需以下划线开头的函数/方法名称。
直接出./system/core/CodeIgniter.php
:
/*
* ------------------------------------------------------
* Security check
* ------------------------------------------------------
*
* None of the functions in the app controller or the
* loader class can be called via the URI, nor can
* controller functions that begin with an underscore
*/
$class = $RTR->fetch_class();
$method = $RTR->fetch_method();
if ( ! class_exists($class)
OR strncmp($method, '_', 1) == 0
OR in_array(strtolower($method), array_map('strtolower', get_class_methods('CI_Controller')))
)
{
show_404("{$class}/{$method}");
}
只需将您的函数定义为私有并在函数名称之前放置一个uderscore,例如:
class Some_class extends CI_Controller{
private function _some_method(){}
}
相反,我会将其设为辅助函数。为此,请在您的 . 中放置一个文件system/application/myval_helper.php
,然后在其中添加您的函数。然后只需调用:
$this->load->helper('myval');
您将能够访问该功能。另一种选择是简单地使用.htaccess
来阻止该确切的 URL。