简体   繁体   中英

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. How do i get around that while using an include? Or is it even possible to do that?

You could do something like this. include() is not magic, but just another function.

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. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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