简体   繁体   English

用户访问控制获取用户级别

[英]user access control getting user level

I have a login form with <form method="POST" action="formdata.php"> 我有一个带有<form method="POST" action="formdata.php">的登录表单
In formdata.php I have the variables as formdata.php中,我的变量为
$n=$_POST['name']; $p=$_POST['password'];

Then after succesful login I direct to the page "main.php" . 然后,成功登录后,我将定向到页面“ main.php”

From main.php I want to check user level and based on user level restrict the pages that a user can access. 我要从main.php检查用户级别,并根据用户级别限制用户可以访问的页面。

To get user level I included 为了获得用户级别,我将其包括在内

$query=mysql_query("SELECT level FROM member WHERE  userName='$n'");
$level=mysql_fetch_array($query);
if ($level==3){
echo "level 3 user";
    }

But I don't think I get the user level from the database to the variable $level correctly as it doesn't executes if ($level==3) . 但是我不认为我可以正确地将用户级别从数据库传递到变量$ level,因为if ($level==3)不会执行。

Can't I use $n in WHERE userName='$n' because $n is declared in formdata.php ? 我不能在WHERE userName='$n'使用$n ,因为$ n在formdata.php中声明了吗?

How can I get the level field value from the database? 如何从数据库中获取级别字段值?

Your usage is wrong. 您的用法是错误的。 It must be 一定是

if($level[0] == 3) 

or 要么

if($level['level'] == 3) 

You should look into Sessions 你应该看看会议

When validating the login in formdata.php, save the user's data into session: 在formdata.php中验证登录名时,将用户的数据保存到会话中:

<?php
session_start();
// validate user credentials
$_SESSION['user_level'] = 3;
$_SESSION['user_name']  = 'John Smith';

You can then access this data in your main.php 然后,您可以在main.php中访问此数据

<?php
session_start();
echo("Hello $_SESSION[user_name]!");

if ($_SESSION['user_level'] > 3) {
    echo "You have admin access";
} else {
    echo "You're a regular user";
}

Just remember to call session_start() on every page where you need to access session data. 只要记住要在需要访问会话数据的每个页面上调用session_start()即可。 Also read up on the subject before using my sample, it's only a starting point. 在使用我的样本之前,还请仔细阅读该主题,这只是一个起点。

mysql_fetch_array return array don't use it as a string mysql_fetch_array返回数组不要将其用作字符串

this condition would be 这种情况是

$level=mysql_fetch_array($query);
if ($level['level']==3){
     echo "level 3 user";
    }

The problem is here '$n' 问题出在这里'$ n'

check this : $query=mysql_query('SELECT level FROM member WHERE userName="'.$n.'"'); 检查以下内容:$ query = mysql_query('从成员WHERE userName =“'。$ n。'”')中选择级别;

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

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