I would like to be able to redirect a user to a different page based on their login name. At the moment, the code below redirects all users to the same page (role.php) no matter what their login is. I have two users: student and tutor. Both of these users are redirected to role.php.
However, I would like to redirect student user to role.php and tutor user to record.php. I thought about doing a couple of if statements if member_id is 1, then redirect to role.php and if member_id is 2, then redirect to record.php. But was unable to do so.
Table structure for members table from where login credentials are checked is as follows:
member_id: 1 or 2
login: student, tutor
passwd: separate password for each user – encrypted with md5.
Any help would be greatly appreciated. Thanks.
//Create query
$qry="SELECT * FROM members WHERE login='$login' AND passwd='".md5($_POST['password'])."'";
$result=mysql_query($qry);
//Check whether the query was successful or not
if($result) {
if(mysql_num_rows($result) == 1) {
//Login Successful
session_regenerate_id();
$member = mysql_fetch_assoc($result);
$_SESSION['SESS_MEMBER_ID'] = $member['member_id'];
session_write_close();
header("location: role.php");
exit();
}else {
//Login failed
header("location: login-failed.php");
exit();
}
}else {
die("Query failed");
}
EDIT: Switch statement used but now redirects to record.php for all users.I think I’m not using the switch statement correctly.
$login="student";
$login="tutor";
switch ($login)
{
case "student":
header("location: role.php");
break;
case "tutor":
header("location: readonlystu.php");
break;
}
I’d go for an switch statement on the role.