Home » Php » redirecting users/admin to their own page

redirecting users/admin to their own page

Posted by: admin October 26, 2017 Leave a comment

Questions:

I have a database table which has 5 records

Usernam    Password
admin       admin
abc123      abc123
xyz123      xyz123
adam        adam
john        john

The admin has his own page which is for add/update/delete.
The other records are of different users which have their own profile.
How will I know that which user is logged in so that I can redirect them to their own profile page.?
Their may be more records in the table in future.

The redirection should happen after searching the entered username and password from the database table. If it does not find any records, then no user found. If yes, then corresponding user’s profile page should be shown or admin’s.

Any kind of help will be appreciated. Thank You !!!

Answers:

this code will help you

if ($_SESSION['Usernam'] == "admin") {
     header('Location: admin_dashboard.php');
     exit();
}else{ 
     header('Location: user_dashboard.php');
     exit();
}

Questions:
Answers:

For this situation, there should be some userroles like administrator and a user.

Every user record will be assigned a user role (it can me multiple roles, for that you need to create a third table to handle m2m relation)

and based on that user role, you can redirect dashboards

userroles (id, title)
users (id, name, email, password, username, userrole_id)

in code, you need to verify username and password and then check the userrole_id.

If its for admin, then redirect to adminDashboard.php with session which contains user record.

Same for other users.

Questions:
Answers:

Add a new column in your table named “type” or “usertype”. after adding the new column set type of user as admin or user. when you try to log in using the username and password check for a value in matched record’s column named “type” added earlier if the value is admin redirect the user to the admin page and if it is a user then redirect it to the user page. If no record is found redirect it to the login page again. Also, create the session before redirecting.