Reputation: 133
I have a PHP/MySQL based website with folders for member pages and folders for admin pages. I want to direct the users to the different pages according to how they login - as a member or as a admin (from the main user pages) - this is the function I've tried and it doesn't work.
How can I write a function that will work for this?
function connectadmin($level) {
if ($level === "Administrator"){
include('admin/home.php');
}elseif ($level === "Member"){
include('member/home.php');
}
}
connectadmin($level);
Upvotes: 0
Views: 900
Reputation: 1361
Well, you should redirect your users, not include files:
// At beggining of this file insert this line
// Start session
session_start();
function connectadmin($level) {
if ($level === "Administrator"){
// Set user role
$_SESSION['role'] = 'Administrator';
// Redirect user
header('Location: admin/home.php');
exit();
}elseif ($level === "Member"){
// Set user role
$_SESSION['role'] = 'Member';
// Redirect user
header('Location: member/home.php');
exit();
}
}
// $level should be something you retrieve from your Database for example
// And perhaps, should be 'Administrator' or 'Member' following your example
connectadmin($level);
And after redirect user don't forget to validate if the logged in user have access to the redirected page.
Edit: For example, if you want to validate if user is Administrator and have access to the page admin/home.php, do something like this:
// You should get from your database, some file or use sessions,
// in your function I have used sessions, so lets use them here too
// At beggining of your file use this
session_start();
// If user is not Administrator
if($_SESSION['role'] !== 'Administrator'){
// It's not admin, let redirect him to somewhere else or show him a Access not allowed page
header('Location: accessNotAllowed.php');
exit();
}
Upvotes: 2