以不同的用户级别登录

时间:2016-10-21 01:45:17

标签: php mysql session

我尝试使用1表单以不同的用户级别登录,因此我尝试使用正确的用户和密码(用户级别和管理员级别)登录,然后将其重定向到http://localhost/dashboard/(当它直接指向索引时.html如果是高级用户或直接到index.php,如果级别管理员)但是当我尝试输入错误的用户名和密码时,它成功重定向到index.php。

这是我的代码:

session_start();

include "koneksi.php";

$username = $_POST['username'];
$password = $_POST['password'];

$query = mysqli_query($koneksi, "SELECT * from user WHERE log_usr='$username' AND pas_usr='$password'");
$exitCount=mysqli_num_rows($query);
    if($exitCount==1){
            $data = mysqli_fetch_array($query);
            $id = $row['log_usr'];
            $lvl = $row['sts_usr'];

            if ($lvl=='A')
            {
                $link = '../index.html';
            }
            elseif($lvl='U')
            {
                $link = '../index.php';
            }
        $_SESSION['username'] = $username;
        header ('location:".$link.'');
        exit();
    }else{
        echo "<script>alert('Username dan Password tidak valid.'); window.location = 'index.php'</script>";
    }

2 个答案:

答案 0 :(得分:0)

请注意Keywords除非您使用反引号,否则不应将其用作column names

$query = mysqli_query($koneksi, "SELECT * from user WHERE log_usr='$username' AND pas_usr='$password'");

应该是:

$query = mysqli_query($koneksi, "SELECT * from `user` WHERE log_usr='$username' AND pas_usr='$password'");

有关KeywordsReserved Words的列表,请点击此处:

10.3 Keywords and Reserved Words

并处于ELSE条件

else{
    echo "<script>alert('Username dan Password tidak valid.'); window.location = 'index.php'</script>";
}

即使输入无效,您仍然会将它们重定向到index.php吗?

答案 1 :(得分:0)

我只是意识到使用变量行获取用户和密码数据,即使我已经从db获取用户和密码的可变数据,这就是为什么if else永远不会工作,因为它没有获得值用户和密码然后直接到elseif条件。

session_start();

include "koneksi.php";

$username = $_POST['username'];
$password = $_POST['password'];

$query = mysqli_query($koneksi, "SELECT * from user WHERE log_usr='$username' AND pas_usr='$password'");
$exitCount=mysqli_num_rows($query);
    if($exitCount==1){
            $data = mysqli_fetch_array($query);
            $id = $data["log_usr"];
            $lvl = $data["sts_usr"];

            if ($lvl=="A")
            {
                $link = 'index.html';
            }
            elseif($lvl="U")
            {
                $link = 'index.php';
            }
        $_SESSION['username'] = $username;
        echo $link;
        header ("location:$link");
        exit();
    }else{
        echo "<script>alert('Username dan Password tidak valid.'); window.location = 'index.php'</script>";
    }