【问题标题】:creating two different sessions in PHP在 PHP 中创建两个不同的会话
【发布时间】:2012-07-02 05:58:23
【问题描述】:

我正在尝试创建两个单独的会话 - 一个用于如果用户是管理员,另一个用于如果用户是作者。 $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>

非常感谢您。

【问题讨论】:

  • 此时您的$_SESSION 中有什么内容?如果它完全为空,我不会感到惊讶,因为我在设置变量的页面上看不到session_start()(每个访问会话的页面都需要这个调用)。
  • 顺便说一句,在不知道正确密码的情况下通过简单的 SQL 盲注即可轻松登录。尝试例如用户名:admin'--
  • 你真的不需要这样做。是的,您的代码中存在 SQL 注入漏洞,请参阅 stackoverflow.com/questions/332365/…bobby-tables.com

标签: php session session-variables


【解决方案1】:

尝试为您的权限使用角色。

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

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

【讨论】:

  • 没错,但更多信息会有所帮助。
  • 你是对的。在这种情况下,我的回答只是一个想法如何让它变得更好。在手机上也很难解释。
  • 是的,我给 +1 是因为我同意你的观点,但从问题和代码来看,OP 可能不知道你所说的角色是什么意思 :)
【解决方案2】:

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

【讨论】:

  • 谢谢马尔西奥。这有帮助。但我还有一个问题。每次我测试时,我都会为任何作者或任何管理员获得相同的会话 ID。在我看来不合适。应该是这样吗?
  • @user1479431,每个会话都必须有一个唯一的 ID,可能您并没有在创建另一个会话之前销毁该会话。您必须使用session_destroy(),将其放在文件末尾welcome.php 仅用于测试目的。
  • 或者使用不同的浏览器。您也可以删除浏览器cookie
  • @user1479431,在调用 session_destroy() 之前,你必须在同一个文件中调用 session_start(),你这样做了吗?
  • 是的!我在welcome.php的末尾添加了session_destroy()(不是在第一部分,因为当时我不需要销毁会话)
【解决方案3】:

我认为,您的代码没有接缝。 我看不到你在哪里调用数据库 还有你在里面有什么

这就是你解决问题的方法

  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>';

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

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

【讨论】:

    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-13
      • 2018-05-04
      • 2013-08-23
      • 2019-10-08
      • 1970-01-01
      • 2018-10-29
      • 2014-09-22
      • 2016-03-21
      相关资源
      最近更新 更多