Search code examples
phpsessionusergroups

Displaying my content for two defined user groups


I have two user groups - Admin (A) and Moderator (M) - and a navigation menu. Some of the links are only to be visible for the Admin, while the others are visible to both. Here's my current code:

<? if($_SESSION["LogedInAdminId"] && $_SESSION['AdminStatus']=="M") { ?>
    <ul>
        <li>...</li>
        <li>...</li>

        <? if($_SESSION["LogedInAdminId"] && $_SESSION['AdminStatus']=="A") { ?>
            <li>...</li>
            <li>...</li>
        <? { ?>

    </ul>
<? } ?>

Currently, the menu is only shown for moderators because of the first line:

<? if($_SESSION["LogedInAdminId"] && $_SESSION['AdminStatus']=="M") { ?>

So I tried to include the Admin group to is like so:

<? if($_SESSION["LogedInAdminId"] && $_SESSION['AdminStatus']=="M" || "A") { ?>

However, this breaks the code and displays the menu for everyone - Admin, Mod, guests, etc. What would be the correct way of including two user groups?


Solution

  • Firstly, make sure the session was started for all pages using sessions.

    Now this syntax || "A" isn't correct. You need to add a new condition.

    Here's what you need to do, and bracketing the second condition:

    if( $_SESSION["LogedInAdminId"] 
        && ($_SESSION['AdminStatus']=="M" || $_SESSION['AdminStatus']=="A")
      )