0

I have developed a login page using PHP which is used by teachers and students for log-in. After login , I can create a session variable to keep him/her logged in until he logs out.

$_SESSION['id']=12;

Now when they log-in for the first time I want them to enter extra information , by providing them with different forms depending on whether he is teacher or student.

Now my question is how will I identify the type of user during his session? What changes do I need to make in Session variable or what extra information do I need to store? (I already have created the database with default passwords for all teachers and students and now need to enter extra information from them as I described).

4
  • store a flag to tell you if the person is a student or not Commented Jun 25, 2013 at 13:55
  • Well this depends on how you can differentiate students from teachers ... Commented Jun 25, 2013 at 13:55
  • 1
    possible duplicate of Role-based access to pages in PHP Commented Jun 25, 2013 at 13:56
  • 1
    you probably need to know who is who before they login Commented Jun 25, 2013 at 13:57

4 Answers 4

2

If you can retrieve the user's information on the fly from the database, the best thing to do (if there are only two roles and not extensive permissions) is add a role field to your users table.

Make it a boolean, so that 0 = student, and 1 = teacher.

You would then check for this using an if() statement to decide which form to display, e.g.

if($user_data['role'] == 0){
    // Display student form
} elseif($user_data['role'] == 1) {
    // Display teacher form
}

You could store this in a $_SESSION['role'] variable if you don't want to have to get this from the database every time you reload the page.

Sign up to request clarification or add additional context in comments.

2 Comments

:is $user_data['role'] an array containing fetched user information? Remember I have different tables for them as they have different types of data. How will I identify the table from which I have to fetch user's role? A work_around is that I can create another table which contains the user_id and user_role.But is there any other way?
Exactly, $user_data would be an array containing essential user info directly from the database, like: $user_data['name'] $user_data['email'] etc.. So what we would do is add an extra column in both the students and teachers tables called "role". In mySQL (or whatever database you're using), in the students table, you would want to make the default value of role 0. In the teachers table, the default value should be 1. There are more efficient ways around it, but this is a quick and dirty method if you need something that just works.
0

There are numerous ways to achieve this. If you’re storing the user ID in the session, then you can look up the user’s details and permissions based on their ID. So if you have a user_type column in the database table where you store whether the user is a teacher, student, goblin or whatever, then you can check the value of this in your PHP script.

Comments

0

If you need to save the infomations:username,password,teacher or student as flag in session,you can do flow,after log in,you save $_SESSION['username'], $_SESSION['password'], $_SESSION['flag'],then,the sessions will be as string,and saved in session file.

2 Comments

:I liked your idea.So I can insert these three lines on the log-in page to store three session variables.?
what would be the point of this? If the database is properly setup, you should be able to pull this information using the user ID being saved in the $_SESSION['id'] field. Unless that's not a user ID.
0

Let's make this easy, i rather change my database, add one more column named "access" or "privilege", 1 for the teachers and 2 for the student, you can filter the status, that define which menu should showed up.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.