1

我需要一些关于重定向的帮助。我有这段代码,想将用户重定向到特定页面。包含用户名和密码的表还包含第三条数据,即组织名称。每个组织都有自己的页面。--我在第 17 行添加了一家公司的名称,但我一直在底部得到回声。任何帮助都会很棒。

$dbc = dbConnect();

$username = "exampleuser";
$sql = "SELECT 'password' from users where 'username' = '$username';";
$result = mysql_query($sql, $dbc); //run the query
    if ($result)
{
        $row = mysql_fetch_assoc($result);
        $password = $row['password'];
         //   echo '<h2>Result Found '.$password.'</h2>'.PHP_EOL;
        $myPassword = $_POST['password'];
        $row = mysql_fetch_assoc($result);
        //$password = $row['password'];
        //match passwords
            if($myPassword == $password){
                //Decide where to send user depending on company column:
                $privilege = $row['company'];
                if($privilege == "companyname"){
                    header( 'Location: admin1.php' );
                }
                if($privilege == "user"){
                    header( 'Location: user.php' );
                }
                if($privilege == "other"){
                    header( 'Location: other.php' );
                }
        exit; //This tells php to stop executing as soon as we redirect
    }else{
        echo "Wrong username/password, try again";
    }

}
4

1 回答 1

0

基本上 php 中的任何重定向都是使用该header函数完成的

例如,这样的调用:

header( 'Location: http://google.com' );

将用户重定向到 google.com

知道这条信息后,我们将根据您从数据库中获取的信息使用它来重定向用户,

假设我们从一个名为的 POST 变量中输入了用户密码myPassword

$myPassword = $_POST['password'];
$row = mysql_fetch_assoc($result);
    $password = $row['password'];
    //match passwords
    if($myPassword == $password){
        //Decide where to send user depending on privilege column:
        $privilege = $row['privilege'];
        if($privilege == "admin"){
            header( 'Location: admin.php' );
        }
        if($privilege == "user"){
            header( 'Location: user.php' );
        }
        if($privilege == "other"){
            header( 'Location: other.php' );
        }
        exit; //This tells php to stop executing as soon as we redirect
    }else{
        echo "Wrong username/password, try again";
    }
}
else
{
    echo '<h2>No user found, or Error: '.mysql_error().' </h2>';
}
于 2013-10-06T18:45:38.597 回答