php - 根据用户类型重定向到另一个页面

标签 php mysql

<?php
    session_start();
    include("connection.php");
    if(isset($_GET['submit'])) {
        mysql_connect('localhost','root','') or die(mysql_error());
        mysql_select_db('awnb') or die(mysql_erroe());
        $Email=$_GET['LoginEmail'];
        $password=$_GET['LoginPassword'];
        if($Email!=""&&$password!="") {
            $query=mysql_query("select * from users where Email='".$Email."' and Password='".$password."'") or die(mysql_error());
            $res=mysql_fetch_row($query);
            if($res) {
                $userType = $res['Type'];
                if($userType == 'user') {
                    $_SESSION['Email']=$Email;
                    header("location:profileuser.php");
                    exit;
                } else if($userType == 'sec') {
                    $_SESSION['Email']=$Email;
                    header("location:profile.php");
                    exit;
                }
            } else {
                echo "
                <script type='text/javascript'>
                alert('Username or Password is incorrect');
                </script>";
                header("location:index.php");
            }
        } else {
            echo "
            <script type='text/javascript'>
            alert('Enter both Username and Passowrd');
            </script>";
            header("location:index.php");
        }
    }
?>
  • 在此代码中,Type 是表中的一个属性,它表示用户的类型,有两种类型的用户称为 user 或 sec。如果类型是用户,它应该重定向到 profileuser.php,否则重定向到 profile.php,但我无法根据用户类型重定向页面。

最佳答案

为了回答您的问题,您正在获取具有 numeric index 的行:

mysql_fetch_row() fetches one row of data from the result associated with the specified result identifier. The row is returned as an array. Each result column is stored in an array offset, starting at offset 0.

如果想得到关联数组,需要mysql_fetch_assoc .

也就是说,您的代码有太多错误,您可能应该重新开始:

  • mysql_* 函数已弃用;
  • 你有一个 SQL injection问题;
  • 您正在存储纯文本密码;
  • 您在 header 重定向之前回显输出;
  • 您使用 GET 而不是 POST 来发送敏感的登录数据。

您可以在 SO 上找到有关每个点的大量信息,因此我不打算重复,但正如我所说,您可能应该重新开始。

关于php - 根据用户类型重定向到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29459814/

相关文章:

c# - Visual C# 如何使使用我的程序的人不必安装 MySql 框架

phpmyadmin无法导出程序

mysql - 不等于 - MySQL 明确没有和组

php - 如何从 php 与 MySQLi 保持连接

php - 在 PHP 中,如何找到一个数组中不存在于另一个数组中的元素?

mysql - 数据库 - 设计一个 "Events"表

php - 用 PHP 访问 MySQL 字段的 Comments

mysql - 连接到mysql数据库时出错,haskell

php - 使用 Azure Active Directory 连接对 PHP 应用程序进行身份验证

php - 如何避免在mysql php中多次插入相同的记录