简体   繁体   English

根据用户角色包含文件

[英]Include file based on user role

I have a script that after a user logs in, it will determine whether he is in one of the following groups: 我有一个脚本,该脚本在用户登录后将确定他是否属于下列组之一:

admin
user
manager

and based on that it will do an include based on his role. 并以此为基础,根据他的角色进行收录。 so lets say if the user is in the admin group it would be like this: 所以可以说,如果用户在管理员组中,它将是这样的:

after login
include( "admin/index.php" );

That works fine, but the issue is when that page comes up any of the links in that page that should go to pages in the admin/ directory but they don't, it still assumes its in root because thats where the include is from. 效果很好,但是问题是当该页面出现在该页面中应转到admin /目录中的页面上的所有链接中的任何链接时,它们却没有,它仍然假定其根目录是因为那是include的来源。 How do i get around that while using an include? 使用include时如何解决? Or is it even possible to do that? 还是有可能做到这一点?

You could do something like this. 你可以做这样的事情。 include() is not magic, but just another function. include()不是魔术,而是另一个函数。

set_include_path ("/path/to/the/files");

switch ($user_role)
{
case 'admin':    include("admin/index.php");         break;
case 'user':     include("meremortal/index.php");    break;
case 'manager':  include("prince/index.php");        break;
}

When your scripts are executed from different directory levels, include() needs to be aware of what directory level you are at in order to use paths relatively. 从不同目录级别执行脚本时, include()需要知道您所处的目录级别,以便相对使用路径。 Instead, I usually define a constant in a config file with the absolute root path on the server, so you don't need to use relative paths: 相反,我通常在配置文件中使用服务器上的绝对根路径定义一个常量,因此您不需要使用相对路径:

const ROOTPATH = "/var/www/yoursite/";

include ROOTPATH."path/to/file.php";

For any href s on the page, you'll need to link to their absolute path as well, using a / at the beginning: 对于页面上的任何href ,您还需要在其开头使用/链接到其绝对路径:

<a href='/path/to/file.php'>link</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM