简体   繁体   中英

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.

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.

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.

Controller:

$this->ion_auth->logged_in() ? $data['logged'] = true : $data['logged'] = false;

$this->load->view('index', $data);

View:

<?php if ($logged): ?>
    do something...
<?php else: ?>
    do something else...
<?php endif; ?>

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