davidcriniti Posted January 30, 2013 Share Posted January 30, 2013 Hi everyone, I'm in the process of developing a site for my school which allows students and teachers to log in to do different things. At the moment I'm working on redirecting people from a page that requires them to be logged in as teachers. I've got a bit of code that works already which directs people away from the page and gives them a message. That code is here: <?php session_start(); if (empty($_SESSION['userName']) || $_SESSION['member_type'] == 'Student' ) { $_SESSION['msg'] = 'You must be a teacher to access the teachers.php page'; header("Location: login_required.php"); die(); } ?> However, I'd like to tweak this a bit so the message is customised, based on whether the person is: i) currently logged in as a student ii) not logged in at all iii) or else, if they're logged in as a teacher, simply displaying the page as normal. The code I've tried is this: <?php session_start(); if (empty($_SESSION['userName']) ) { $_SESSION['msg'] = 'You must be a teacher to access the teachers.php page. You are currently not logged in.'; header("Location: login_required.php"); die(); } else if ($_SESSION['member_type'] == 'Student'); { $_SESSION['msg'] = 'You are logged in as a student, but you must be a teacher to access the teachers.php page'; header("Location: login_required.php"); die(); } ?> This works by giving the correct messages to people who are either students or who are not logged in. However, if I am logged in as a teacher, I am still given the message saying "You are logged in as a student" etc...rather than having access to the page. I'd be greatly appreciative if anyone could point out my error. Thanks greatly, Dave Quote Link to comment https://forums.phpfreaks.com/topic/273815-if-statement-to-handle-sessions/ Share on other sites More sharing options...
requinix Posted January 30, 2013 Share Posted January 30, 2013 The code you posted is exactly the code you have? Exactly? Print out $_SESSION var_dump($_SESSION); and see if anything else (besides the member_type) looks wrong. Quote Link to comment https://forums.phpfreaks.com/topic/273815-if-statement-to-handle-sessions/#findComment-1409093 Share on other sites More sharing options...
Love2c0de Posted January 30, 2013 Share Posted January 30, 2013 Not sure if it will make any difference but you have ended your if else statement with a semi-colon. Delete the semi-colon. Kind regards, L2c. Quote Link to comment https://forums.phpfreaks.com/topic/273815-if-statement-to-handle-sessions/#findComment-1409101 Share on other sites More sharing options...
davidcriniti Posted January 31, 2013 Author Share Posted January 31, 2013 Thanks Love2cOde. Removing the semi-colon fixed it! Quote Link to comment https://forums.phpfreaks.com/topic/273815-if-statement-to-handle-sessions/#findComment-1409288 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.