redirecting user after login to different pages depending on their role

Hi all,
I’m very new to php and just started learning it. I am trying to make a website that is going to have different groups of users. I have a mysql database right now that stores the users’ username, password, and training_group. I would like to be able to redirect the user once they log on, to their specific homepage that is going to depend on which training_group they have been assigned to (I will be directly adding in their information into the database.) I have the website working to a point where I can login but now, I just need to be able to figure out where / how to implement the code to redirect people based on their training_group. Any help is greatly appreciated!

Right now I have a file, login.php:
[php]

<?php session_start(); error_reporting(0); require'database/connect.php'; require'functions/general.php'; require'functions/users.php'; if (logged_in() === true) { $session_user_id =$_SESSION ['user_id']; $user_data = user_data($session_user_id, 'user_id', 'username', 'password', 'first_name', 'last_name', 'email', 'class', 'training_group', 'week'); } $errors = array(); if (empty($_POST) === false) { $username = $_POST['username']; $password = $_POST['password']; if (empty($username) === true || empty($password) === true) { $errors[] = 'please input a username and password first! '; } else if (user_exists($username) === false) { $errors[] = 'We could not locate you in our database!'; } $login = login($username, $password); if ($login === false) { $errors [] = 'That username/password combination is incorrect'; } else { $_SESSION['user_id'] = $login; header('Location:logged_in/templates/logged_in_home.php'); exit (); } } else { $errors [] = 'No data received'; } include 'includes/overall/header.php'; if (empty ($errors) === false) { ?>
<h2>We tried to log you in, but...</h2>
<?php echo output_errors($errors); } include 'includes/overall/footer.php'; ?>

[/php]

a file users.php:
[php]

<?php function user_data($user_id) { $data = array(); $user_id = (int)$user_id; $func_num_args = func_num_args(); $func_get_args = func_get_args(); if ($func_num_args > 1) { unset($func_get_args[0]); $fields = '`' . implode ('`,`',$func_get_args) . '`'; $data = mysql_fetch_assoc(mysql_query("SELECT $fields FROM `users` WHERE `user_id` = $user_id")); return $data; } } function logged_in() { return(isset($_SESSION['user_id'])) ? true : false; } function user_exists($username) { $username = sanitize($username); return (mysql_result($query = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'"), 0) == 1) ? true : false; } function user_id_from_username($username) { $username = sanitize($username); return mysql_result(mysql_query("SELECT `user_id` FROM `users` WHERE `username` = '$username'"), 0, 'user_id'); } function login ($username,$password) { $user_id = user_id_from_username($username); $username = sanitize($username); $password = md5($password); return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username' AND `password` = '$password'"),0) == 1) ? $user_id : false; } ?>

[/php]

I am pretty new at this to but I think this would work (probably not the prettiest)

Basically after login you want a different Location in
[php]header(‘Location:logged_in/templates/logged_in_home.php’);[/php]

So that could be done in several ways with a file name stored in the database and returned by your login script alternatively if it is a small number of options you could have an extra database field just storing a permissions number read that and then do an if then to set the header.

Headers can be used if no output to the browser is sent already.
Or used javascript location to send you to the page you want the user to go to.
You will have to know which type of user to know which page to send them to so after loggin I guess you have soemthing like usertype as a set variable or in an array of some kind, that can tell the code which type of user the user is to forward to another page.

do an if then else statement looking at the $user_data=>training_group data and using the header redirect line from the previous reply to direct them to the correct page.

Sponsor our Newsletter | Privacy Policy | Terms of Service