【问题标题】:How to redirect into different page by user type in php and mysql如何通过用户类型在php和mysql中重定向到不同的页面
【发布时间】:2014-09-12 22:24:09
【问题描述】:

我想从同一个登录页面重定向用户。

文件夹结构...

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

用户表

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

如果登录用户的用户类型是 admin,那么他/她将被重定向到 /administration/admin.php 否则 /application/app.php。

我该怎么做?

问候,

【问题讨论】:

  • 只需读取用户的 user_type 并为每个用户设置一个 header 位置 if 语句?
  • 在谷歌 PHP 重定向中搜索。
  • @AndyHolmes 感谢您的回复。我应该先告诉你,我是 php 新手。所以我需要一些代码示例。
  • @Raj 请查看我的回答以获取帮助
  • @Raj 你能查看我的回答并提供反馈吗? :)

标签: php mysql redirect usertype


【解决方案1】:

试试这样的:

<?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']; 正在从您的数据库中模拟您的SELECT,我不会为您编写连接脚本,但这只是您需要研究的指南。此外,请查看 PDO 或 MySQLi,因为 MySQL 已被弃用。

【讨论】:

  • 投反对票的人,你能评论一下你为什么投反对票吗?
【解决方案2】:

检查用户名和密码后。在这种情况下,您可以检查他们的用户类型并将其保存到变量 $user_type。

$user_type = $row['user_type'] //从你数据库的表行中获取用户的用户类型

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

    }
}

【讨论】:

  • 至少使用user_type OP 的概述
猜你喜欢
  • 1970-01-01
  • 2016-11-07
  • 2011-12-29
  • 1970-01-01
  • 1970-01-01
  • 2011-11-06
  • 2011-06-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多