【问题标题】:Access level and Authentication (PHP/MYSQL)访问级别和身份验证 (PHP/MYSQL)
【发布时间】:2015-02-04 08:41:23
【问题描述】:

我的这个脚本运行良好。但是我对 utype_id 可以访问其他 utype_id 有问题。如何对其进行身份验证以使utype_id=1 无法访问utype_id=2?代码如下。

<?php
session_start();

include('includes/connection.php');

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

if(!empty($username) && !empty($password))
{

    $command="select * from user WHERE  username = '".$username."' and password='".$password."'";

    $result1=mysql_query($command);
    $count=mysql_num_rows($result1);

    $utype_id = "SELECT utype_id FROM user WHERE username='$username'";
    $result2 = mysql_query($utype_id);
    $result3 = mysql_fetch_row($result2);

    if($count==0)
    {
        header("location:loginform.php?attempt=fail");
    }
    else {
        $sql="select * from user WHERE username='".$username."'";
        $result=mysql_query($sql);
        while($row=mysql_fetch_row($result)){

            $_SESSION["id"]=$row[0];
            $_SESSION["username"]=$row[5];
            $_SESSION["name"]=$row[2];

            switch($result3[0]){

            case '1':
                header("location: module1/index.php");
                break;

            case '2':
                header("location: module2/index.php");
                break;

            case '3':
                header("location:loginform.php?attempt=unauthorized");
                break;
            }
        }
    }
}
else
{
    header("location:loginform.php?attempt=null");
}
?>

【问题讨论】:

  • 为什么你在三个查询中查询相同的用户详细信息??
  • 1 查询是否为用户的选择。 2 用于用户的 utype_id。和 3 用于组合 fetch_row。这行得通。但问题是 utype_id 1 可以访问 utype_id 2。这不应该是..
  • 请检查我下面的代码,希望对你有帮助

标签: php mysql login login-script access-levels


【解决方案1】:

按照代码剪切额外的查询和长脚本

            include('includes/connection.php');

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

            $location = 'loginform.php?attempt=null';

            if(!empty($username) && !empty($password))
            {

            $command="select * from user WHERE  username = '".$username."' and password='".$password."'";
            $result=mysql_query($command);

            $location = 'loginform.php?attempt=fail';
            if(mysql_num_rows($result) > 0 {
                $frUser = mysql_fetch_array($result, MYSQL_BOTH);
                $_SESSION["id"]=$frUser['id']; // Change the name here
                $_SESSION["username"]=$frUser['username'];// Change the name here
                $_SESSION["name"]=$frUser['name'];  // Change the name here
                $utypeId = $frUser['utype_id'];
                switch($utypeId) {
                case '1':
                    $location = 'module1/index.php';
                break;
                case '2':
                    $location = 'module2/index.php';
                break;
                case '3':
                    $location = 'loginform.php?attempt=unauthorized';
                break;
                }   
            }
            }
            header("location:".$location);
            ?>

【讨论】:

  • 我试试这个.. 模块 1 和 2 的每个 index.php 上是否有任何身份验证?得到和错误解析错误:语法错误,第 16 行 D:\WAMP\www\ptoms\login.php 中的意外 '{'
  • 每个用户一个模块,如果是,则在用户会话中保留 utype_id 并验证用户直接访问未经授权的模块
  • 是的,它的 per usertype 1 适用于其他类型的用户。 2 适用于另一种类型的用户。每个用户都有特定的用户类型。如果他/她是用户类型 1,他可以访问模块 1。如果他/她是用户类型 2,他可以访问模块 2。是的,他们都可以访问模块 1 和模块 2,这对我来说是个大问题:(
  • 最好使用sperate table来维护用户模块。您也可以使用逗号分隔值来维护多个值
  • 我不知道。但脚本运行良好。我的问题是用户 1 可以访问用户 2 模块,反之亦然。其他都很好。
猜你喜欢
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 2019-05-01
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 2020-08-25
  • 1970-01-01
相关资源
最近更新 更多