Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何创建不可访问的控制器操作?_Php_Codeigniter_Codeigniter 2 - Fatal编程技术网

Php 如何创建不可访问的控制器操作?

Php 如何创建不可访问的控制器操作?,php,codeigniter,codeigniter-2,Php,Codeigniter,Codeigniter 2,在我的一个控制器中,我使用codeigniter的表单验证助手。我的验证规则之一是我创建的一个自定义函数,它返回true或false。 问题是,这个验证函数是在控制器的类中声明的。如何防止用户浏览我的验证函数作为控制器操作(mysite/mycontroller/my_callback_func) 注意-尝试将函数设置为私有,但我收到一个错误,验证帮助程序无法访问该函数。我将改为将其设置为帮助程序函数。为此,在系统/application/myval_helper.php中放入一个文件,然后在其

在我的一个控制器中,我使用codeigniter的表单验证助手。我的验证规则之一是我创建的一个自定义函数,它返回true或false。 问题是,这个验证函数是在控制器的类中声明的。如何防止用户浏览我的验证函数作为控制器操作(mysite/mycontroller/my_callback_func)


注意-尝试将函数设置为私有,但我收到一个错误,验证帮助程序无法访问该函数。

我将改为将其设置为帮助程序函数。为此,在
系统/application/myval_helper.php
中放入一个文件,然后在其中添加函数。然后打电话:

$this->load->helper('myval');

您将能够访问该功能。另一种选择是简单地使用
.htaccess
来阻止该确切的URL。

我会将其作为一个助手函数。为此,在
系统/application/myval_helper.php
中放入一个文件,然后在其中添加函数。然后打电话:

$this->load->helper('myval');

您将能够访问该功能。另一种方法是简单地使用
.htaccess
来阻止该确切的URL。

只需在函数/方法名称的开头加下划线即可

直接从
/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}");
    }

只需以下划线开始函数/方法名称

直接从
/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}");
    }

只需定义您的函数private,并在函数名称前放置一个uderscore,例如:

class Some_class extends CI_Controller{

     private function _some_method(){}
}

只需定义您的函数private,并在函数名称前放置一个uderscore,例如:

class Some_class extends CI_Controller{

     private function _some_method(){}
}

我也是这样做的,效果很好。文档中也概述了这一点:我也是这样做的,效果很好。文件中也对其进行了概述: