简体   繁体   English

codeigniter身份验证:重定向到主页

[英]codeigniter authentication: redirecting to home page

I am new to codeigniter framework. 我是Codeigniter框架的新手。 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. 在某些浏览器/ Web服务器组合中,我发现必须设置页面的页眉才能从服务器实际重新加载。 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: 您可以在htaccess中进行设置,但可以在CI语法中进行设置,然后在标头中输出HTML:

$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');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM