我的PHP会话变量没有设置

时间:2013-07-13 02:12:05

标签: php session

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

目前,无论登录信息如何,都会在登录后分配来宾。

<?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;
    }
}
?>

如何获取它以便正确分配这些变量? 谢谢!

1 个答案:

答案 0 :(得分:1)

您的问题来源:if($type == "author")

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