简体   繁体   中英

Conditional display links based upon the login status PHP

I have a small site which has 200 members. Below is code for the login/logout links which display a "Change Password", "Report" and "Logout" link when logged in. While not logged in, the "Login" and "Forgot password" links are displayed.

Recently we ran a competition which had 14 winners and what I am trying to achieve is to put a link into the code that only the 14 winners can see and not the remaining members.

I'm not quite sure where to start, is it possible to put a condition in this code for the 14 user ids/email addresses or would I be better off putting a new field into the user's database? Any help or push in the right direction would be appreciated!

     <?php # loginnav.php> 

     // Display links based upon the login status. 

     // Show LOGIN links if this is the LOGOUT page. 

      if (isset($_SESSION['user_id']) 
      AND (substr($_SERVER['PHP_SELF'], -10) 
      !='logout.php')) 

      { echo 
      '<li><a href="logout.php">Logout</a></li>
       <li><a href="change_password.php">Change Password</a></li> 
       <li><a href="report.php">Report</a></li>  
     '; } else { 

     // Not logged in.   
     echo 
     ' <li><a href="login.php">Login</a></li> 
     <li><a href="forgot_password.php">Forgot Password?</a></li> 
      '; } ?>

you can simply put the winners' id in an array , then check if the user id is in that array for showing the link.

$winners = array(1, 2, 3, 4, 5);

if (in_array($id, $winners))
{
    echo "link";
}
     $winners_array = array('userid1', 'userid2', 'userid3', 'userid4', ...);  
          // This array contains users IDs who are winners
          // You can write it manualy right intj the login file, 
          //include it from external file or form from your Data Base

    if (isset($_SESSION['user_id']) 
      AND (substr($_SERVER['PHP_SELF'], -10) 
      !='logout.php')) 
      { 
      echo 
      '<li><a href="logout.php">Logout</a></li>
       <li><a href="change_password.php">Change Password</a></li> 
       <li><a href="report.php">Report</a></li>  
      '; 
      if(in_array($_SESSION['user_id'], $winners_array)){
         // If current ID is in winners list we add special link for him
         echo '<li><a href="winer_page.php">Winner link</a></li>';
      }
      } else { 
     // Not logged in.   
     echo 
     ' <li><a href="login.php">Login</a></li> 
     <li><a href="forgot_password.php">Forgot Password?</a></li> 
      '; } ?>

One option is to add a conditional check of the user id and if it matches your list of ids, add the link. The downside of this block of code is that it is hardcoded, and can become a maintenance issue if you plan more contests or other links unique to certain members in the future. (You would end up with several of these blocks of code)

First set your winner ids into an array, like so:

$winningIds = array(1,2,3,4,5,6,7,8,...);

Then in the echo block where you are printing the links do this:

if (in_array($_SESSION['user_id'], $winningIds))
{
     echo '<li><a href="newlink.php">New Link</a></li> ';
}

Edit: I realized I didn't mention the other option I was thinking, which is to store a list of 'unique' links in the database for each user. Then after your echo block, you'd print the unique links for each user.

I envision this as two additional tables. Table 1 would be 'links' and would have three columns - id, link and display text. Table 2 would be 'user_links' and would contain two columns - linkId and userId.

You'd join the tables and have the links (which is your href ) and display text display that are associated in the user_links table.

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