Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter Ion_auth check login

how can i check if user logged in or not in view ??

something like this:

if ($this->ion_auth->logged_in())
{
    // do something ..
}
else
{
    // do something else ..
}

and how can i get user data to variable ?

thanks a lot.

like image 675
MAMProgr Avatar asked Mar 04 '26 18:03

MAMProgr


2 Answers

In your controller, you can do something like

function some_method() {
    if ($this->ion_auth->logged_in()) {
      $loggedin = true;
    } else {
      $loggedin = false;
    }

    $data['loggedin'] = $loggedin;

    $this->load->view('some_view.php', $data);
}

and then in your view

<?php if ($loggedin): ?>
    <p>Logged in</p>
<?php else: ?>
    <p>Please log in</p>
<?php endif; ?>


Alternatively, you could just load a different view from your controller if the user is logged in or not.

like image 135
stealthyninja Avatar answered Mar 06 '26 08:03

stealthyninja


As stealthyninja said, it should be performed in the controller. You can cut down on the amount of code in your controllers by extending the base CodeIgniter controller, and then have your controller for authenticated areas of your project extend your custom controller. Your custom controller could have a constructor that checks whether or not the user is authenticated and route them appropriately. This same constructor could also set variables within so as to be easily accessible by the custom controller's subclasses.

like image 30
27A Avatar answered Mar 06 '26 07:03

27A



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!