Jump to content

if statement to handle sessions


davidcriniti

Recommended Posts

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.