phpmysqlhttp-redirectusertype

How to redirect into different page by user type in php and mysql


I want to redirect the users from the same log-in page.

folder structure...

root
|
--------/application/app.php <---------- for normal user
|
--------/administration/admin.php <------- for admin user
|
-------- index.php (Log-in page)

User Table

ID       USER_NAME       PASSWORD      USER_TYPE
1         admin           pass@admin    admin
2         user            pass@user     user

If the login user's user type is admin then he/she will be redirected to /administration/admin.php else /application/app.php.

How can I do it ?

Regards,


Solution

  • After checking the username and password. You can check their usertype and save it to a variable in this case $user_type.

    $user_type = $row['user_type'] //get the usertype of the user from table row in your database

    if( $user == 1){ //check if user or password is correct from query
    
    
     if($user_type == "normal user"){ //check usertype
    
        header("Location:/application/app.php"); //if normal user redirect to app.php
    
        }else{
    
        header("Location:/administration/admin.php"); //if admin user redirect to admin.php
    
        }
    }