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?

3 Answers 3

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

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

Cheers!

Sign up to request clarification or add additional context in comments.

5 Comments

Exactly what the OP asked for, although I think it should be elaborated, because it is clear he doesn't have a good grasp on the language so it's vital to understand why an array and what it actually is.
It's returning syntax error now :P in your if statement. it's saying unexpected '[' perhaps something with escaping characters? I'm not sure.
make sure you typed the session part correctly, but other than that it should work properly. Escaping is not necessary here, so just make sure you typed it out exactly the way I have it written.
It was my bad, I was correct though. It was with escaping characters. I was echoing HTML code which had quote brackets inside it, which I guess I could prevent from happening with using the single quote on the echo statement. How do I select this as an answer? I'm new here ^^. And thanks!
Found it, hehe it's really late.
0

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.

1 Comment

Aye, I know. Users are being stored in a file with start_session() after being logged in, and this php file is always being included in all pages.
0

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!

1 Comment

Many good outcomes will arise from that, no doubt. Good luck learning!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.