0

我正在尝试创建两个单独的会话——一个用于用户是管理员,另一个用于用户是作者。$type 存储类型为枚举(可以是作者或管理员)。但是我的代码甚至为管理员创建了作者会话。我是 PHP 和 MySQL 的新手。有人可以告诉我代码中的错误在哪里。

<?php
include("dbconnect.php");
$con= new dbconnect();
$con->connect();
//create and issue the query
$sql = "SELECT type FROM users WHERE username = '".$_POST["username"]."' AND password = PASSWORD('".$_POST["password"]."')";

$result = mysql_query($sql);

//get the number of rows in the result set; should be 1 if a match
if (mysql_num_rows($result) == 1) {
   $type_num=0;
    //if authorized, get the values
      while ($info = mysql_fetch_array($result)) {
    $type =$info['type'];
    }

     if($type == "admin")
        {
         $_SESSION['type']=1;
         $u = 'welcome.php';
         header('Location: '.$u);  
        }
       else
        {
          $_SESSION['type']=$type_num;
          $u = 'welcome.php';
          header('Location: '.$u);


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

我的welcome.php 如下

<?php
  session_start();
?>

<html>
<body>
<h2>Welcome.</h2>
<?
if($_SESSION['type']==1){
     echo "You are of the usertype Admin and your session id is ";
     echo session_id();
}
else {
echo "You are of the usertype Author and your session id is ";
echo session_id();
}
?>



</body>
</html>

非常感谢你。

4

4 回答 4

1

尝试使用角色来获得您的权限。

一般来说,您只有一个会话。我的意思是您没有两个名为_SESSION 的变量。

使用角色的概念,您可以简单地检查用户是否有权做某事。

于 2012-07-02T06:03:14.800 回答
0

在会话中注册 var 之前,您必须调用session_start()代码的第一部分$_SESSION['type']

于 2012-07-02T06:03:00.167 回答
0

没有你的代码接缝很好,我想。我看不到你在哪里调用数据库以及你在那里有什么

所以这就是你如何解决问题

  while ($info = mysql_fetch_array($result)) {
      $type =$info['type'];
      echo $type . '<br />';
  }

或者

  echo '<pre>';
  while ($info = mysql_fetch_array($result)) {
      $type =$info['type'];
      print_r($info);
  }
  echo '</pre>';

如果您从未在其中看到管理员,并且它必须是“管理员”而不是管理员或管理员;那么问题出在您的数据库中。您没有管理员定义或拼写正确的管理员。

顺便一提。看看我格式化得有多好。这样更容易阅读。
如果您不这样做,编码人员将不会查看您的代码。

于 2012-07-02T06:28:44.947 回答
0

尝试使用session_regenerate_id();方法来创建不同的会话 ID。

于 2012-08-10T14:30:52.423 回答