0

我需要三种不同的用户类型:访客、作者和管理员。当用户注册时,他们通过下拉列表选择他们是作者还是管理员。这三种不同的类型具有不同的功能。

目前,无论他们是否登录,都会在他们登录后分配访客。

<?php
include("dbconnect.php");
$con = new dbconnect();
$con->connect();

session_start();

if (isset($_POST['submit']))
    {
        $sql = "SELECT type FROM Users WHERE username = '".$_POST["username"]."' AND         password = ('". sha1($_POST["password"]) ."')";

        $result = mysql_query($sql);

    if (mysql_num_rows($result) == 1) {
        $type_num=0;
        while ($info = mysql_fetch_array($result)) {
            $type =$info['type'];
        }

        if($type == "admin")
        {

            $_SESSION['type']=2;
            $_SESSION['username']= $_POST["username"];
            header("Location: viewPosts.php");
        }
        if($type == "author")
        {

            $_SESSION['type']=1;
            $_SESSION['username']= $_POST["username"];
            header("Location: viewPosts.php");
        }
        else 
        {
            $_SESSION['type']= $type_num;
            $_SESSION['username']="guest";
            header("Location: viewPosts.php");
        }


    } else {
        //redirect back to login form if not authorized
        header("Location: loginfailed.html");
        exit;
    }
}
?>

我如何得到它,以便正确分配这些变量?谢谢!

4

1 回答 1

1

你的问题的根源:if($type == "author")

将其更改为elseif ($type == "author"). 这将使条件检查线程适当地继续到最后。否则,如果$type == "admin",那么它将始终调用else您在那里的最后一条语句。

于 2013-07-13T02:18:06.490 回答