Al Kasih
Al Kasih

Reputation: 886

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.

Upvotes: 0

Views: 69

Answers (4)

Nitin Pund
Nitin Pund

Reputation: 1092

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.

Upvotes: 1

Roh&#236;t J&#237;ndal
Roh&#236;t J&#237;ndal

Reputation: 27242

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 } ?>

Upvotes: 1

Sougata Bose
Sougata Bose

Reputation: 31749

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
}

Upvotes: 1

Manish Jangir
Manish Jangir

Reputation: 5437

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 } ?>

Upvotes: 1

Related Questions