使用登录数据来引导用户流量

时间:2013-10-06 18:33:34

标签: php

我需要一些重定向的帮助。我有一段代码,并希望将用户重定向到特定页面。包含用户名和密码的表还包含第三个数据,即组织名称。每个组织都有自己的页面。 - 我在第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";
    }

}

1 个答案:

答案 0 :(得分:0)

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

完成的

例如,这样的调用:

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

会将用户重定向到google.com

根据您从数据库中获取的信息,我们将使用该信息来重定向用户,

假设我们在名为myPassword

的POST变量中输入了用户密码
$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>';
}