Raja
Raja

Reputation: 792

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,

Upvotes: 1

Views: 9101

Answers (2)

Andy Holmes
Andy Holmes

Reputation: 8077

Try something like this:

<?php

$userType = $row['user_type'];

if($userType == 'admin'){
    header("Location: /administration/admin.php"); // This line triggers a redirect if the user_type is admin
} else {
    header("Location: /application/app.php"); // This line triggers for other user_types
}

?>

$row['user_type']; is simulating your SELECT from your database, I won't write the connection script for you, but this is just a guideline into what you need to look into. Also, look into PDO or MySQLi as MySQL is deprecated.

Upvotes: 2

John Robertson Nocos
John Robertson Nocos

Reputation: 1485

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

    }
}

Upvotes: 1

Related Questions