将参数传递给 php include/require 构造

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3710831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:48:31  来源:igfitidea点击:

passing parameters to php include/require construct

phpparametersincluderequire

提问by JoJoeDad

I've read quite a few posts that are very similar to the question I'm about to ask, but I just wanted to be sure that there wasn't a more sophisticated way to do this. Any feedback is greatly appreciated.

我已经阅读了很多与我要问的问题非常相似的帖子,但我只是想确定没有更复杂的方法来做到这一点。非常感谢任何反馈。

I want to create a mechanism to check whether or not a logged-in user has access to the php script that is currently being called. If so, the script will continue on; if not, the script just fails out using something like die('you have no access').

我想创建一种机制来检查登录用户是否有权访问当前正在调用的 php 脚本。如果是这样,脚本将继续;如果没有,脚本就会使用类似die('you have no access').

I came up with two ways of accomplishing this:

我想出了两种方法来实现这一点:

(please assume my session stuff is coded/working fine - i.e. I call session_start(), set up the session vars properly and etc)

(请假设我的会话内容已编码/工作正常 - 即我调用 session_start(),正确设置会话变量等)

  1. Define a global variable first, then check the global variable in a required header file. For example:

    Content of current_executing_script.php:

    // the role the logged in user must have to continue on   
    $roleNeedToAccessThisFile = 'r';
    require 'checkRole.php''
    

    Content of checkRole.php:

    if ($_SESSION['user_role'] != $roleNeedToAccessThisFile) die('no access for you');
    
  2. Define a function within the header file and call the function immediately after including/requiring it:

    Content of checkRole.php:

    function checkRole($roleTheUserNeedsToAccessTheFile) {
        return ($_SESSION['user_role'] == $roleTheUserNeedsToAccessTheFile);
    }

    Content of current_executing_script.php:

    require 'checkRole.php';
    checkRole('r') or die('no access for you');
  1. 先定义一个全局变量,然后在需要的头文件中查看全局变量。例如:

    current_executing_script.php 的内容:

    // 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');
    
  2. 在头文件中定义一个函数并在包含/需要它后立即调用该函数:

    checkRole.php 的内容:

    function checkRole($roleTheUserNeedsToAccessTheFile) {
        return ($_SESSION['user_role'] == $roleTheUserNeedsToAccessTheFile);
    }

    current_executing_script.php 的内容:

    require 'checkRole.php';
    checkRole('r') or die('no access for you');

I'm wondering if there is a way to basically just pass a parameter to checkRole.php as part of the include or require construct?

我想知道是否有一种方法可以基本上将参数传递给 checkRole.php 作为 include 或 require 构造的一部分?

Thanks in advance.

提前致谢。

回答by Spudley

There isn't a way to pass parameters to include or require.

没有办法传递参数以包含或要求。

However the code that is included joins the program flow at the point where you include it, so it will inherit any variables that are in scope. So for example if you set $myflag=true immediately before the include, your included code will be able to check what $myflag is set to.

然而,包含的代码在您包含它的地方加入了程序流,因此它将继承范围内的任何变量。因此,例如,如果您在包含之前立即设置 $myflag=true,您包含的代码将能够检查 $myflag 设置为什么。

That said, I wouldn't suggest using that technique. Far better for your include file to contain functions (or a class) rather than code that gets run straight off. If you've included a file containing functions then you can call your functions with whatever parameters you want at any point in your program. It's much more flexible, and generally a better programming technique.

也就是说,我不建议使用该技术。让包含文件包含函数(或类)而不是直接运行的代码要好得多。如果您包含了一个包含函数的文件,那么您可以在程序中的任何位置使用您想要的任何参数调用您的函数。它更加灵活,并且通常是一种更好的编程技术。

Hope that helps.

希望有帮助。

回答by CodeUK

Include with parameters

包含参数

This is something I've used on my recent Wordpress project

这是我在最近的 Wordpress 项目中使用的东西

Make a function functions.php:

做一个功能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 parameters in cards-block.php:

获取参数cards-block.php

// $parameters is within the function scope
$args = array(
    'post_type' => $parameters['query'],
    'posts_per_page' => 4
);

Call the template index.php:

调用模板index.php

get_template_partial('cards-block', array(
    'query' => 'tf_events'
)); 

If you want a callback

如果你想要回调

For example, the total count of posts that were displayed:

例如,显示的帖子总数:

Change functions.phpto this:

改成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; 
} 

Change cards-block.phpto this:

改成cards-block.php这样:

// $parameters is within the function scope
$args = array(
    'post_type' => $parameters['query'],
    'posts_per_page' => 4
);
$callback = array(
    'count' => 3 // Example
);

Change index.phpto this:

改成index.php这样:

$cardsBlock = get_template_partial('cards-block', array(
    'query' => 'tf_events'
)); 

echo 'Count: ' . $cardsBlock['count'];

回答by eDriven_Levar

In the past, many people have disagreed with this approach. But I think it is a matter of opinion.

过去,许多人不同意这种方法。但我认为这是一个见仁见智的问题。

You can't pass _GET or _POST param via a require() or include() , but you can you first set a _SESSION key/value and pull it on the other side.

您不能通过 require() 或 include() 传递 _GET 或 _POST 参数,但您可以先设置一个 _SESSION 键/值并将其拉到另一侧。

回答by kmuenkel

You could have the required file return an anonymous function, and then call it immediately after.

您可以让所需的文件返回一个匿名函数,然后立即调用它。

//required.php

$test = function($param)
{
    //do stuff
}

return $test
//main.php
$testing = require 'required.php';
$testing($arg);