将参数传递给php include/require构造
我已经读过很多与我将要问的问题非常相似的帖子,但我只是想确定没有更复杂的方法可以做到这一点。非常感谢您的反馈 我想创建一种机制来检查登录用户是否有权访问当前被调用的php脚本。如果是,脚本将继续;如果没有,脚本将使用类似于将参数传递给php include/require构造,php,parameters,include,require,Php,Parameters,Include,Require,我已经读过很多与我将要问的问题非常相似的帖子,但我只是想确定没有更复杂的方法可以做到这一点。非常感谢您的反馈 我想创建一种机制来检查登录用户是否有权访问当前被调用的php脚本。如果是,脚本将继续;如果没有,脚本将使用类似于die(“您没有访问权限”)的方法失败。 我想出了两种方法来实现这一点: (请假设我的会话代码/工作正常-即,我调用session_start(),正确设置会话变量等) 首先定义一个全局变量,然后检查所需头文件中的全局变量。例如: 当前_执行_script.php的内容: //
die(“您没有访问权限”)的方法失败。
我想出了两种方法来实现这一点:
(请假设我的会话代码/工作正常-即,我调用session_start(),正确设置会话变量等)
// the role the logged in user must have to continue on
$roleNeedToAccessThisFile = 'r';
require 'checkRole.php''
checkRole.php的内容:
if ($_SESSION['user_role'] != $roleNeedToAccessThisFile) die('no access for you');
require 'checkRole.php';
checkRole('r') or die('no access for you');
提前感谢。没有方法传递要包含或需要的参数 但是,所包含的代码在包含它的位置加入程序流,因此它将继承范围内的任何变量。因此,例如,如果在include之前设置$myflag=true,则包含的代码将能够检查$myflag设置为什么 也就是说,我不建议使用这种技术。与直接运行的代码相比,include文件包含函数(或类)要好得多。如果您包含了一个包含函数的文件,那么您可以在程序中的任何时候使用任何参数调用函数。它更灵活,通常是一种更好的编程技术 希望对您有所帮助。包括参数 这是我在最近的Wordpress项目中使用的东西 创建一个函数
functions.php
:
function get_template_partial($name, $parameters) {
// Path to templates
$_dir = get_template_directory() . '/partials/';
// Unless you like writing file extensions
include( $_dir . $name . '.php' );
}
get_template_partial('cards-block', array(
'query' => 'tf_events'
));
在cards block.php中获取参数:
// $parameters is within the function scope
$args = array(
'post_type' => $parameters['query'],
'posts_per_page' => 4
);
调用模板index.php
:
function get_template_partial($name, $parameters) {
// Path to templates
$_dir = get_template_directory() . '/partials/';
// Unless you like writing file extensions
include( $_dir . $name . '.php' );
}
get_template_partial('cards-block', array(
'query' => 'tf_events'
));
如果你想回电话
例如,显示的帖子总数:
将functions.php
更改为:
function get_template_partial($name, $parameters) {
// Path to templates
$_dir = get_template_directory() . '/partials/';
// Unless you like writing file extensions
include( $_dir . $name . '.php' );
return $callback;
}
$cardsBlock = get_template_partial('cards-block', array(
'query' => 'tf_events'
));
echo 'Count: ' . $cardsBlock['count'];
将cards block.php
更改为:
// $parameters is within the function scope
$args = array(
'post_type' => $parameters['query'],
'posts_per_page' => 4
);
$callback = array(
'count' => 3 // Example
);
将index.php
更改为:
function get_template_partial($name, $parameters) {
// Path to templates
$_dir = get_template_directory() . '/partials/';
// Unless you like writing file extensions
include( $_dir . $name . '.php' );
return $callback;
}
$cardsBlock = get_template_partial('cards-block', array(
'query' => 'tf_events'
));
echo 'Count: ' . $cardsBlock['count'];
过去,许多人不同意这种做法。但我认为这是一个意见问题
不能通过require()或include()传递_GET或_POST参数,但可以先设置一个_SESSION键/值,然后从另一侧拉取它 您可以让所需的文件返回一个匿名函数,然后立即调用它
//required.php
$test = function($param)
{
//do stuff
}
return $test
谢谢Spudley,所以听起来你觉得我的方法2才是最好的选择。再次感谢。很好,先生@CodeUkNo只是不。。假设您已经在使用会话。。只需使用全局变量或作用域变量您就否决了我的答案,但用户已经这么做了。我刚刚离开了代码库,用户似乎已经理解了。接受的答案建议用户使用方法2,其中checkRole.php检查已经设置的会话变量。正是我所说的。