I see some serious problems with your script:
- Don't use mysqli - it's deprecated and shouldn't be used. Use PDO instead.
- You should hash your passwords. Read about
password_hash().
Don't create SQL statements by conca3. You concatenating string from user input. It is vulnerable to SQL injection. You should use placeholders.
$stmt = $db->prepare("SELECT * FROM user WHERE uid=:uid AND password=:password");
$stmt->execute(array(':password' => $password, ':uid' => $uid));
$rows = $stmt->fetchAll();
If you want a more detailed review of your code you can post on codereview.
As for implementation of roles, you could create an additional table ROLE with single column NAME. For example (in MySql):
CREATE TABLE role (
name varchar(255) NOT NULL
)
ALTER TABLE role ADD PRIMARY KEY ('name');
INSERT INTO role (name) VALUES
('ADMIN'),
('STUDENT');
Then you could add an additional column to your user table with foreign key to table roles.
ALTER TABLE user ADD (
role varchar(255) NOT NULL,
FOREIGN KEY(role) REFERENCES role(name)
)
Ten you could just fetch the name of the role and redirect to the right location.
if($row['ROLE'] == "ADMIN"){
header("Location: http://example.com/admin.php");
die();
} else {
....
}
What are the advantages of this approach? You have all your roles listed in one table and because of a foreign key, you will preserve data integrity. In column ROLE of the USER table, you will be able to store the only role which is also stored in the ROLE table. If you would try to remove role which is assigned still assigned to any user database wouldn't allow it.
This answer assumes your user could have only one role. If you want to be able to assign multiple roles to your user, you would have to learn how to map many-to-many relationship in the database.