【问题标题】:passing parameters to php include/require construct将参数传递给 php include/require 构造
【发布时间】:2011-04-12 06:27:26
【问题描述】:

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

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

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

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

  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');

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

提前致谢。

【问题讨论】:

    标签: php parameters include require


    【解决方案1】:

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

    //required.php
    
    $test = function($param)
    {
        //do stuff
    }
    
    return $test
    
    //main.php
    $testing = require 'required.php';
    $testing($arg);
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 不只是不..假设您已经在使用会话..只需使用全局或范围变量
      • 您对我的回答投了反对票,但这就是用户已经在做的事情。我刚刚离开了代码库,而用户似乎已经理解了。接受的答案建议用户方法号 2,其中 checkRole.php 正在检查已设置的会话变量。正是我所说的。
      【解决方案3】:

      包含在参数中

      这是我在最近的 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' );
      } 
      

      获取cards-block.php中的参数:

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

      调用模板index.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; 
      } 
      

      把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 更改为:

      $cardsBlock = get_template_partial('cards-block', array(
          'query' => 'tf_events'
      )); 
      
      echo 'Count: ' . $cardsBlock['count'];
      

      【讨论】:

      • 很好,@CodeUk 先生
      【解决方案4】:

      没有办法将参数传递给 include 或 require。

      但是,包含的代码会在您包含它的位置加入程序流,因此它将继承范围内的所有变量。因此,例如,如果您在包含之前立即设置 $myflag=true,您的包含代码将能够检查 $myflag 的设置。

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

      希望对您有所帮助。

      【讨论】:

      • 谢谢 Spudley,所以听起来你觉得我的方法 #2 是要走的路。再次感谢。
      猜你喜欢
      • 2015-10-25
      • 2012-01-18
      • 1970-01-01
      • 2018-02-13
      • 1970-01-01
      • 1970-01-01
      • 2014-10-23
      • 2023-04-04
      • 2015-10-11
      相关资源
      最近更新 更多