简体   繁体   中英

Show menus based on user logged in

I have this session start in every pages, which the function is of course as to check if the member has login or not. If they are not login, they will direct to the login page.

 <?php
      session_start();
      if(empty($_SESSION['login_id'])){          
      header("Location: login.php");
      exit();
    }
  ?> 

But now I need to check if member has login or not, if they had not, they will only find some menu in the top. Like this:

If they have not logged in

        <ul id="topRight-link">
        <li><a href="#"><img src="">Live Chat</a></li>
        <li><a href="#"><img src="">Referral</a></li>
        <li><a href="#"><img src="">Login</a></li>        
        </ul>

If they have

         <ul id="topRight-link">
           <li><a href="#"><img src="">Live Chat</a></li>
        <li><a href="#"><img src="">Referral</a></li>
        <li><a href="#"><img src="">Logout</a></li>        
        </ul>

May I know how to do this with the session I have? Thanks for the help.

You can use the following code:

<?php
if(empty($_SESSION['login_id'])){
?>
<ul id="topRight-link">
        <li><a href="#"><img src="">Live Chat</a></li>
        <li><a href="#"><img src="">Referral</a></li>
        <li><a href="#"><img src="">Login</a></li>        
        </ul>
<?php } else { ?>
 <ul id="topRight-link">
           <li><a href="#"><img src="">Live Chat</a></li>
        <li><a href="#"><img src="">Referral</a></li>
        <li><a href="#"><img src="">Logout</a></li>        
        </ul>
<?php } ?>

Try -

<?php
if(empty($_SESSION['login_id'])){ 
?>
<ul id="topRight-link">
    <li><a href="#"><img src="">Live Chat</a></li>
    <li><a href="#"><img src="">Referral</a></li>
    <li><a href="#"><img src="">Login</a></li>        
</ul>
<?php
} else {
?>
<ul id="topRight-link">
     <li><a href="#"><img src="">Live Chat</a></li>
     <li><a href="#"><img src="">Referral</a></li>
     <li><a href="#"><img src="">Logout</a></li>        
</ul>
<?php
}

Try this it will work :

<?php
if(empty($_SESSION['login_id'])){
?>
<ul id="topRight-link">
        <li><a href="#"><img src="">Live Chat</a></li>
        <li><a href="#"><img src="">Referral</a></li>
        <li><a href="#"><img src="">Login</a></li>        
        </ul>
<?php } else { ?>
 <ul id="topRight-link">
           <li><a href="#"><img src="">Live Chat</a></li>
        <li><a href="#"><img src="">Referral</a></li>
        <li><a href="#"><img src="">Logout</a></li>        
        </ul>
<?php } ?>

You can achieve the expected result by doing the following procedure

1) In your Database Table put user role field 2) When user logs in, retireve the user role 3) Using retrieved User role and switch case statement you can redirect the user on different Pages which contains required menus

For Example

If User Role is Customer, Then in switch case you can redirect it to customer menu.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM