简体   繁体   中英

codeigniter authentication: redirecting to home page

I am new to codeigniter framework. I am trying to build a authentication system.

Authentication is working fine. My problem is that after successfully logging, when I click the back button in the browser it is directed to login page again. I want to redirect it to the home page itself. i want to reload the home page not the index page(index page is the login page, after successful login goes to home page)

How can I do it

Thanks in advance

Use redirect in your controller:

function index()
{
    if ($this->session->userdata['logged_in']) // or whatever you use
    {
        redirect ('/controller/home');
    }
    else
    {
         // show login - post login form to /controller/do_login
    }
 }
 function do_login()
 {
     // check login form and set user session
     redirect('/controller/home'); // or index - does the same...
 }
 function home()
 {
     // show home page
 }

This simple example shows how to check if user is logged in prior to outputting the login screen, then use redirect in the controller.

In some browsers/web server combinations I found having to set the headers for the page to actually reload from the server. Otherwise the result could be that pressing back actually shows the login screen even if logged in; no query is made to the server. You can set this in htaccess but possible in CI syntax, before outputting HTML in your header:

$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
$this->output->set_header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); 

Sets your page to always reload on back-presses.

after login successful you can use

if ($this->login())
{
    //redirect them to the index page
    redirect('/', 'refresh');
}

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