1

I would like to display admin button on the menu only if the $_SESSION['user'] is equal to say a variable with $admins (which I would like to store several values as in several users).

I know I can do this through SQL, but I would like to know if this is possible through PHP and if so how.

edit: to be more specific I know I can do it by looking up the database for a user matching the admin group, but I do not have this setup.

Even if SQL is the easiest option I really hope there is a way with PHP, I was reading about PHP_AUTH_USER quite a bit, but I'd like to store several users in a variable, if that is possible?

4

3 回答 3

2
$admins = array("username1","username2","username3");
session_start();

if(in_array($_SESSION["user"],$admins))
{
    //do stuff
}

干杯!

于 2012-12-03T01:26:29.590 回答
0

听起来您在问哪些替代位置可以存储用户列表,以及他们拥有哪些权限(管理员或非管理员)。

您可以: * 将其硬编码到 PHP 文件中(非常简单!) * 将其放入配置文件中(ini、xml,随你命名)

无论如何..您需要将此列表放在“某处”。它不会留在 PHP 内存中,所以这个地方需要是 PHP 可以从中加载它的地方。

您不能只输入一个变量(如 $_SESSION)并期望留在那里。所以与数据库(确实是一个合乎逻辑的选择)一起,其他两个是您的选择。

于 2012-12-03T01:26:13.960 回答
0

您正在寻找的是一种称为array. 您可以在这里找到更多信息:http: //php.net/manual/en/language.types.array.php

关键是它是key => value成对结构的。您绝对应该深入研究这一点,因为作为一种数据类型,数组可以让您实现很多事情,例如looping through data,轻松searching获取数据,sorting它等等。

这是一个适用于几乎任何类型的编程语言的基本概念,所以请帮自己一个忙!

于 2012-12-03T01:31:40.027 回答