Home > other >  How can I hide some navbar pages when a non-admin user is logged-in in the admin panel?
How can I hide some navbar pages when a non-admin user is logged-in in the admin panel?

Time:10-30

What I do is to put all these

  • nav-item
  • (there are about five of these links) inside the navbar.php and just add <?php include(navbar.html) in all those five page so that everytime I click page 1,2,3,.4 or 5th webpage, the nav bar still can be seen. But when the "User" (non-admin user) logged in, I don't want to include the page 1 on his navbar because that page has features for admin only, which is what I'm having a problem with....

    Index.php

    <?php
    include('includes/navbar.php');
    ?>
    
    <!-- Main Content  -->
    
    <?php
    include('includes/footer.php');
    ?>
    

    page2.php

    <?php
    include('includes/navbar.php');
    ?>
    
    <!-- Main Content  -->
    
    <?php
    include('includes/footer.php');
    ?>
    

    page3.php

    <?php
    include('includes/navbar.php');
    ?>
    
    <!-- Main Content  -->
    
    <?php
    include('includes/footer.php');
    ?>
    

    page4.php

    <?php
    include('includes/navbar.php');
    ?>
    
    <!-- Main Content  -->
    
    <?php
    include('includes/footer.php');
    ?>
    

    Page5.php

    <?php
    include('includes/navbar.php');
    ?>
    
    <!-- Main Content  -->
    
    <?php
    include('includes/footer.php');
    ?>
    

    code.php Here is the code where for validating if the user is an admin or non-admin user The !password_verify is for validation for password and hash password.

    if(isset($_POST['login_btn']))
    {
        $email_login = $_POST['emaill']; 
        $password_login = $_POST['passwordd']; 
        
        $query = "SELECT * FROM register WHERE email='$email_login' LIMIT 1";
        $query_run = mysqli_query($connection, $query);
        $password = "SELECT password FROM register WHERE email='$email_login'";
        $usertypes = mysqli_fetch_array($query_run);
    
       if($usertypes['usertype'] == "admin")
       {
        if(!password_verify($password_login, $password))
        {   
            $_SESSION['username'] = $usertypes['username'];
            header('Location: index.php');
        }
        else
        {
             $_SESSION['status'] = "Email / Password is Invalid";
             header('Location: login.php');
        }
       } 
       else if  ($usertypes['usertype'] == "user")
       {
        if(!password_verify($password_login, $password)){   
            $_SESSION['username'] = $usertypes['username'];
            header('Location: index2.php');
        }
        else
        {
             $_SESSION['status'] = "Email / Password is Invalid";
             header('Location: login.php');
        }
       }
    }
    

    UPDATE: This line is the solution Just put all the list items

  • inside this php elseif if you want to display a specific navbar list when an Admin user is logged in but want to hide them in a non-admin one.

    <?php if($_SESSION['TYPE'] == 'admin'): ?>
    
    <li class="nav-item">
        <a class="nav-link" href="PageForAdmin.php">
            <i class="fas fa-user" style="font-size: 20px;"></i>
            <span>Page For Admin</span></a>
    </li>
    
    <?php endif;  ?>
    

    CodePudding user response:

    Use if else loop For Laravel

    @if(Auth::user()->user_role == "role")
    // Show page 1
    @else
    // show page 2
    @endif
    

    For you

    <?php
    
    if ($user->role == 'admin'){
    <li class="nav-item">
        <a class="nav-link" href="page1.php">
            <i class="fas fa-fw fa-tachometer-alt"></i>
            <span>Page1</span></a>
    </li>
    
       
     }else {
     <li class="nav-item">
        <a class="nav-link" href="page.php">
            <i class="fas fa-fw fa-tachometer-alt"></i>
            <span>Page2</span></a>
    </li>
    
     }
    
    ?>
    <iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

    May be code not compiled

    <?php
    
    if ($user->role == "admin"){
    

    If You use number then

    <?php
    
    if ($user->role == 1){
    

    Note: This is a example you can use it. I not tested it. Use how you want to use role or usertype at the if condition

    <?php
        
        if ($usertype->username == "admin){
    

    Example For 5 Page

    <?php
    
    if ($user->role == 'admin'){
    <li class="nav-item">
        <a class="nav-link" href="page1.php">
            <i class="fas fa-fw fa-tachometer-alt"></i>
            <span>Page1</span></a>
    </li>
    
       
     }else if ($user->role == 'author'){
     <li class="nav-item">
        <a class="nav-link" href="page.php">
            <i class="fas fa-fw fa-tachometer-alt"></i>
            <span>Page2</span></a>
    </li>
    
     
    
     }else if ($user->role == 'editor'){
     <li class="nav-item">
        <a class="nav-link" href="page.php">
            <i class="fas fa-fw fa-tachometer-alt"></i>
            <span>Page3</span></a>
    </li>
    
      }else if ($user->role == 'superadmin'){
     <li class="nav-item">
        <a class="nav-link" href="page.php">
            <i class="fas fa-fw fa-tachometer-alt"></i>
            <span>Page4</span></a>
    </li>
    
     }else if ($user->role == 'superadmin2'){
     <li class="nav-item">
        <a class="nav-link" href="page.php">
            <i class="fas fa-fw fa-tachometer-alt"></i>
            <span>Page5</span></a>
    </li>
    
     }
    ?>
    <iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

    You can use nested if else or elseif too Like you can use next if in firstone's else like see below code

    <?php
    
    if ($user->role == 'admin'){
      // your query
    
       
     }else 
    
     if ($user->role == 'author'){
       // your query
     }else 
     if ($user->role == 'admin2'){
        // your query
    
     }else 
     if($user->role == 'admin1'){
        // your query
     }
    
     
    
     }
    ?>
    <iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

    • Related