简体   繁体   English

此特权代码有什么问题?

[英]what's wrong in this code of privilege?

how to check privilege in "check"php code or page ?? 如何在“检查” PHP代码或页面中检查特权?

I using explode and in_array 我使用explode和in_array

after the user log in and in "check" page the code must check privilege of user if he has "dataDisplay" privilege or not ..but the code in "check" page doesn't do that 用户登录并进入“检查”页面后,代码必须检查用户是否具有“ dataDisplay”特权..但是“检查”页面中的代码不执行该操作

what's my wrong in "check" page code 我在“检查”页面代码中有什么问题

this is my Database: 这是我的数据库:

+--------------------+-------------------------------+
| username           |   user_privilege              |
|--------------------|-------------------------------|
| amal               |7gz,agt_courses,newbill        | 
|                    |                               |
+----------------------------------------------------+
|                    |                               |
| ahmed              |dataDisplay,previllige,newUsers|
+----------------------------------------------------+

first page "login" php: 第一页“登录” php:

<?php
ob_start();
session_start();
include '../connection/connect.php';

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


if($username && $password ){
    $finduser = mysqli_query($link,"SELECT * FROM LOGIN WHERE username = '".$username."' AND password = '".$password ."'") or die("error");
    if(mysqli_num_rows($finduser) !=0){
        while($row = mysqli_fetch_array($finduser)){
            $uname = $row['username'];
            $pass= $row['password '];
            $arr=explode(",",$row['user_privilege']);
        }
    }
        {
        $_SESSION['sessionname'] =$uname;
        $_SESSION['sessionpass'] =$password ;
        $_SESSION['sessionpre'] =explode(",",$row['user_previllige']);
        header ("location:../agtSite/agt2.php");
    }
} 
ob_end_flush();
?>

second page "check" php: 第二页“检查” php:

<?php
session_start();

$_SESSION['sessionpre']='';

$haspermission =in_array("dataDisplay",$_SESSION['sessionpre']);


if( $haspermission )
{
    header("location: agt2.php");
    }

else{header("location: ../display/display.php");}

?>

You forgot to add else block, a problem you could avoid if you indent your code properly. 您忘记添加else块,如果适当缩进代码,可以避免此问题。 Make sure you create the session only if you find a user 确保仅在找到用户后才创建会话

if(mysqli_num_rows($finduser) !=0){
    while($row = mysqli_fetch_array($finduser)){
        $_SESSION['sessionname'] =$row['username'];
        $_SESSION['sessionpass'] = $row['password '];
        $_SESSION['sessionpre'] = explode(",",$row['user_previllige']);
        header ("location: ../check.php");
    }
}
else{
//could not find user
}

Also as the others pointed out in the comments you to prevent SQL injection . 也正如其他人在注释中指出的那样,您可以防止SQL注入

  1. The else that others are pointing out is a problem. else指出的其他问题。

  2. So is the apparent typo in user_previllige . user_previllige的明显错字也是。

  3. In your else block, you refer to $uname and $row['user_previllige'] , but these things only have meaning in the if block; 在您的else块中,您引用$uname$row['user_previllige'] ,但是这些东西仅在if块中有意义; they are undefined if you are in the else block. 如果您在else块中,则它们是未定义的。

  4. Are you sure you are connected to the database? 您确定已连接到数据库吗? I see $link , but you don't include the code you used to initiate the connection in the code above. 我看到$link ,但是您没有在上面的代码中包含用于启动连接的代码。

  5. As others have pointed out, this is extremely vulnerable to SQL injection . 正如其他人指出的那样,这非常容易受到SQL注入的攻击

  6. Also, don't store passwords in plain text ! 另外, 请勿以纯文本形式存储密码 Salt and hash them. 盐和哈希他们。 Assume that they will be stolen one day, and design your application so it wouldn't matter, because the salted and hashed value would be useless. 假设它们有一天会被盗,并设计您的应用程序就没关系了,因为盐值和哈希值将毫无用处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM