davidcriniti Posted December 20, 2012 Share Posted December 20, 2012 Hi, I've been working on a website for both students and teachers at my school, and I would like to preface this post by saying that I'm a teacher who is just trying to teach himself a bit of php / mysql, so please bear that in mind if I ask questions you may regard as basic. I've got: - some pages which are open to the public, - some which are available to anyone logged in (be they students or teachers), and - some which are available to teachers only. Teachers and students all log in on the same page. If someone goes to a page which requires a log in, I redirect them to the login page (login_required.php) with code like this: <?php session_start(); if (empty($_SESSION['userName']) || $_SESSION['member_type'] == 'Student' ) { header("Location: login_required.php"); die(); } ?> The above code is for a page that requires users to be logged in as a teacher to access. Once this re-direct to login_required.php has occured, is there any way for the information to be passed to the login_required.php page that this person has come from a page which requires a teacher log in, and echo the message "The page you tried to access requires you to log in as a teacher. Please do so now." ...and similarly, if a person comes from another page which simply requires them to be logged in (as either student or teacher), such as the following: <?php session_start(); if (empty($_SESSION['userName']) ) { header("Location: login_required.php"); die(); } ?> ...the message would say "The page you tried to access requires you to be logged in. Please do so now". Any advice on how to achieve the above would be very gratefully appreciated. Thanks for your time, Dave Quote Link to comment Share on other sites More sharing options...
Jessica Posted December 21, 2012 Share Posted December 21, 2012 Put the message into the session before the redirect, then echo it and unset it. Quote Link to comment Share on other sites More sharing options...
davidcriniti Posted December 21, 2012 Author Share Posted December 21, 2012 Thanks for your quick response Jessica. I did it (following your advice) and it seems to have worked from the tests I've done so far. Might just post what I've done in case others may find it useful. If there's anything that's a bit strange about my coding, please feel free to let me know too. On the page that requires users to be logged in as a teacher, I changed the code (from what it was in my initial post) to: <?php session_start(); if (empty($_SESSION['userName']) || $_SESSION['member_type'] == 'Student' ) { /* New code inserted below */ $_SESSION['teacher_required'] = 'You must be a teacher to access this page'; /* End new code inserted */ header("Location: login_required.php"); die(); } ?> Then on the login_required.php page I added the code: <?php echo $_SESSION['teacher_required']; $_SESSION['teacher_required'] = ""; ?> Thanks again Jessica. Cheers, Dave Quote Link to comment Share on other sites More sharing options...
Jessica Posted December 21, 2012 Share Posted December 21, 2012 I would use a key like "msg" or message, makes it more flexible in the future otherwise good! Quote Link to comment Share on other sites More sharing options...
Jessica Posted December 21, 2012 Share Posted December 21, 2012 Actually also before echo you need to check isset(), and use unset instead of = "" Quote Link to comment Share on other sites More sharing options...
davidcriniti Posted December 21, 2012 Author Share Posted December 21, 2012 Thanks for the tips Jessica. When you say change the key to 'msg', am I correct in assuming you are referring to the session name? I've played around and manged to use unset and it seems to work (Never used that before, so thanks!) Can you give me any tip on how to check isset() ? This is the code I now have on the login_required.php page: <?php echo $_SESSION['msg']; unset ($_SESSION['msg'] ) ?> Thanks again, Dave Quote Link to comment Share on other sites More sharing options...
Jessica Posted December 21, 2012 Share Posted December 21, 2012 Look it up in the manual and if you're still stuck post the new code Quote Link to comment Share on other sites More sharing options...
davidcriniti Posted December 21, 2012 Author Share Posted December 21, 2012 Ok, This is the latest code I have: <?php if (isset($_SESSION['msg']) ) { echo $_SESSION['msg']; unset ($_SESSION['msg'] ); } ?> From what I can tell, it works the same as without the "if (isset($_SESSION['msg']) )" line, however, I gather from your advice that there is a good reason for using the isset. What would this be? I looked at the manual here: http://au1.php.net/manual/en/function.isset.php ...and know it's about determing if the variable (or session in this case) has been set, but would be grateful to get my head around why it is important when it seems to work without it. Thanks for all your help, Dave Quote Link to comment Share on other sites More sharing options...
Jessica Posted December 21, 2012 Share Posted December 21, 2012 It's good practice. If you turn on error reporting to show all errors including notices, you'll get an error when you try to load the page without having set that session var Quote Link to comment 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.