user1868565
user1868565

Reputation: 151

PHP storing several users in a variable

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?

Upvotes: 1

Views: 47

Answers (3)

Morgan Wilde
Morgan Wilde

Reputation: 17303

What you're looking for is a data structure called an array. You can find you more here: http://php.net/manual/en/language.types.array.php

The key bit about is that it is structured in key => value pairs. You should definitely dig deeper into this, because as a data type, the array allows you to achieve many things, such as looping through data, easily searching for data, sorting it and so on.

This is a fundamental concept valid for almost any type of programming language, so do yourself a favor!

Upvotes: 0

Liftoff
Liftoff

Reputation: 25392

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

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

Cheers!

Upvotes: 2

Evert
Evert

Reputation: 99523

It sounds like you are asking what alternative places are to store a list of users, and which permissions they have (admin or not).

You could: * hardcode it in a PHP file (very easy!) * put it in a configuration file (ini, xml, you name it)

Regardless.. you need to put this list 'somewhere'. It doesn't stay in PHP memory, so this somewhere place needs to be a place where PHP can load it from.

You cannot just put in a variable (like $_SESSION) and expect to stay there.. So along with a database (indeed a logical choice) those other two are your options.

Upvotes: 0

Related Questions