samedi 22 décembre 2018

If statements and admin privileges in CodeIgniter

I am building a user account system in CodeIgniter, and am having tremendous trouble using session data to lock or unlock links. The following code is based on advice from Stack Overflow (Controlling User Privileges in Codeigniter):

<div id="navbar">
          <ul class="nav navbar-nav">
            <li><a href="<?php echo base_url(); ?>">Home</a></li>
            <li><a href="<?php echo base_url(); ?>about">About</a></li>
            <li><a href="<?php echo base_url(); ?>Bodycontroller">Chapters</a></li>
          </ul>
          <ul class="nav navbar-nav navbar-right">
            <?php if(!$this->session->userdata('logged_in')) : ?>
            <li><a href="<?php echo base_url(); ?>Users/login">Login</a></li>
            <li><a href="<?php echo base_url(); ?>Users/register">Register</a></li>
          <?php endif; ?>
            <?php if($this->session->userdata('user_class') == 1) : ?>
            <li><a href="<?php echo base_url(); ?>Users">User List</a></li>
          <? endif; ?>
          <?php if($this->session->userdata('logged_in')) : ?>
            <li><a href="<?php echo base_url(); ?>Bodycontroller/create">Compose</a></li>
            <li><a href="<?php echo base_url(); ?>Users/logout">Logout</a></li>
          <?php endif; ?>
          </ul>
        </div>

The Compose and Logout links work, but the User List link does not. It fails to display regardless of which user is logged in. I have already verified that the session cookie contains the correct user class.

I am presuming that this is a syntax problem, but the Codeigniter manual has no information on IF statements in this situation. The specific code from Stack Overflow is paraphrased as follows:

      <?php if($this->session->userdata('user_class') == 1) : ?>
        <li><a href="<?php echo base_url(); ?>Users">User List</a></li>
      <? endif; ?>

My sense is that the assignment "== 1" may be in an incorrect form, despite it being copied from Stack Overflow. The CodeIgniter manual is unclear on this. Any assistance in this matter would be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire