eljon_i3
eljon_i3

Reputation: 169

Codigniter: Display current user in homepage after log-in

I want to ask how can I display the current user that has logged-in in my homepage.

Here is my login view:

<form action="login2" method="POST" id="log_form" name="log_form" style="margin-left: auto; margin-right: auto;"> 
                        <div class="modal-body">
                           <div class="container">     
                              <div id="nav_log1">
                                Username: <input type="text" id="txt_user1" name="txt_user1" style="color: black;"/>
                                Password: <input type="password" id="txt_pass1" name="txt_pass1" style="color: black;"/>
                              </div>
                            </div> <!-- /container -->
                        <div class="modal-footer" style="padding: 20px; margin-top: 20px;">
                            <button type="submit" value="login" id="btn_login" class="btn btn-primary btn-sm" name="btn_login">Sign-In</button>
                            <button class="btn btn-warning btn-sm" data-dismiss="modal">Cancel</button>
                        </div>
                        </div>
                     </form>

Here is my model:

public function select_login($value2){
    $login_user = $this->db->select('*')
                           ->from('tblusers')
                           ->where('Username', $value2['txt_user1'])
                           ->where('Password', $value2['txt_pass1'])
                           ->get()->row();
           return $login_user;
}

And here is the controller that corresponds to the action of my form in my view:

 public function login2(){
    $data = array();
    if(!empty($_POST['btn_login'])){

        $session_login = $this->inventory_model->select_login($this->input->post());
        if($session_login){
            $this->session->set_userdata('login_session', array('Username' => $session_login->Username,
                                                                'Password' => $session_login->Password,
                                                                'UserType' => $session_login->UserType));
                                                            redirect('inventorysys_controller/homepage');
        }
        else{
            $this->session->set_flashdata('msg','Invalid Username/Password!');
            redirect('inventorysys_controller/loginpage2');
        }
    }
}

As you can see there is a redirect('inventorysys_controller/homepage'); in my login2 controller. There are two views on that page since I am loading multiple views on my webpage. I will only post the header view since that's where I want to put the name of the user that has logged-in. Here is the view:

<div class="container">
  <div class="masthead">
    <h3 class="text-muted">Inventory System</h3>&nbsp;
    <ul class="nav navbar-default nav-justified">
      <li><a href="<?php echo site_url('inventorysys_controller/homepage'); ?>">Home</a></li>
      <li><a href="<?php echo site_url('inventorysys_controller/inventory'); ?>">Inventory</a></li>
      <li><a href="<?php echo site_url('inventorysys_controller/userslist1'); ?>">Users List</a></li>
      <li><a href="<?php echo site_url('inventorysys_controller/view_employees'); ?>">Employee</a></li>
      <li class="dropdown"><a href="#" class="dropdown-toggle" id="dropdownMenu1" data-toggle="dropdown">Administration<span class="caret"></span></a>
        <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
            <li role="presentation"><a role="menutitem" tab-index="-1" href="<?php echo site_url('inventorysys_controller/maintenance'); ?>">Inventory Maintenance</a></li>
        </ul>
      </li>
      <li><a href="<?php echo site_url('inventorysys_controller/about') ?>">About</a></li>
      <li><a name="btn_logsouts" id="btn_logsouts" href="<?php echo site_url('inventorysys_controller/logsouts') ?>"><span class='glyphicon glyphicon-off'></span>&nbsp;Logout</a></li>
    </ul>
 </div>

Here is a print screen of my homepage view for you to get my question:

enter image description here

So that's it, I just want to select the username that has logged-in and show it on my homepage. I know I will create another query like SELECT * FROM 'tblusers' where 'user' = --blank--. I just don't know how to deal with that yet. Please help me guys! Thank you in advance for your help! :)

Upvotes: 0

Views: 58

Answers (2)

eljon_i3
eljon_i3

Reputation: 169

So I solved it thanks to @Zeeshan.

I just added this code in my View:

<?php $session_login = $this->session->userdata('login_session'); ?>

<label><?php echo $session_login['Username']; ?></label>

I hope this could help you guys. Thank to @Zeeshan!

Upvotes: 0

Zeeshan
Zeeshan

Reputation: 801

As i see you are assigning your username in sessions ,you can access it wherever in your code like this.

echo $this->session->userdata('Username');

Let me know if you have any doubts.

Regards, Zeeshan.

Upvotes: 1

Related Questions