treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Controlling Navigation with PHP Depending on User?

  • I am trying to figure out the best solution to this problem - but not being hot on PHP, i could do with a bit of help.

    I'm building a site for Wordpress.

    the site will have a number of different users who will have different levels of access to read pages. So 'User Group 1' might have access to view page 1, 2, 3 and 4. 'User Group 2' might have access to page 1, 4, 5 and 6.

    What i am trying to figure out is - once 'user group 1' logs in and gets redirected to the index page i want to control which tabs are displayed in the nav bar.

    - if user group 1 - display tabs 1, 2, 3 and 4
    - if user group 2 - display tabs 1, 4, 5, 6

    (however, there may be a large number of different user groups / access levels.)

    and so on.

    Is this achieved with 'switch' ?
    could someone point me in the right direction please.

    many many thanks for your help.
  • can you make the many different navs and then put them in different files and include them? something like

    if($group == 1)
    {
    include('nav1.php');
    }
    else
    {
    include('reg_nav.php');
    }

    Maybe store your groups in an array and make the nav available to the groups in that array?
  • many thanks - i will have a look at this solution. is this the most logic solution?
  • Yeah that solution seems to be very logical. You might want to consider storing the variable in a PHP session, so that you can reuse it. The best way would be to have the logical expression (the if statement) on the login page, and then a variable is set that contains the path of the navigation, so...


    if($usergroup == 1)
    {
    $_SESSION['nav-path'] = \"nav/nav1.php\";
    }
    else
    {
    $_SESSION['nav-path'] = \"nav/nav2.php\";
    }


    And then when your user visits a page...


    <?php include($_SESSION['nav-path']) ?>


    This solution will make it more manageable in the long run. Hope this helps.

    -Tom