将参数传递给php include / require构造 [英] passing parameters to php include/require construct

查看:558
本文介绍了将参数传递给php include / require构造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我想创建一种机制来检查登录用户是否可以访问当前正在使用的php脚本称为即可。如果是这样,脚本将继续;如果没有,脚本就会失败,比如 die('你没有访问')

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

我想出了两种方法来实现这个目标:

I came up with two ways of accomplishing this:

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

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


  1. 首先定义一个全局变量,然后检查所需头文件中的全局变量。例如:

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

current_executing_script.php的内容:

Content of current_executing_script.php:

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

checkRole.php的内容:

Content of checkRole.php:

if ($_SESSION['user_role'] != $roleNeedToAccessThisFile) die('no access for you');


  • 在头文件中定义一个函数,并在包含/要求后立即调用该函数:

  • Define a function within the header file and call the function immediately after including/requiring it:

    checkRole.php的内容:

    Content of checkRole.php:

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

    current_executing_script.php的内容:

    Content of current_executing_script.php:

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


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

    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?

    提前致谢。

    推荐答案

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

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

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

    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.

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

    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.

    希望有所帮助。

    这篇关于将参数传递给php include / require构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆