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

北慕城南 提交于 2019-12-23 00:49:43

问题


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,


回答1:


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

    }
}



回答2:


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.



来源:https://stackoverflow.com/questions/24881616/how-to-redirect-into-different-page-by-user-type-in-php-and-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!