在CodeIgniter的每个页面上保留Tank Auth [英] Keep Tank Auth on every page in CodeIgniter

查看:120
本文介绍了在CodeIgniter的每个页面上保留Tank Auth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题有点难以解释,但我会尝试..



基本上,在 tank_auth 示例脚本有此代码重定向用户,如果他们尚未登录;

  if(!$ this-> tank_auth-> is_logged_in()){
redirect('/ auth / login /');
} else {
$ data ['user_id'] = $ this-> tank_auth-> get_user_id();
$ data ['username'] = $ this-> tank_auth-> get_username();
$ this-> load-> view('welcome',$ data);
}

如果您有登录页面并且用户从头开始每一次。 (我很舒服这样做)



但我想让用户能够跳转到网站(几乎)任何控制器,并有一个登录栏跨越顶部。登录时,它不应将其重定向到另一个页面。



例如,我的用户可能直接加载 example.com/food/burgers 。我想要一个空白页面出来,但只是与顶部的登录栏。然后,当他们登录时,他们最终回到汉堡包页面,但这一次还有一个汉堡的列表,顶部的酒吧,告诉他们他们已经登录,可以退出的选项。 p>

那么我该怎么做呢?我需要从每个控制器调用auth / login方法吗?我做一个包括吗?不知道。

解决方案

首先,您需要创建一个所有控制器都将扩展的基本控制器。您将在此基本控制器中检查身份验证。如果他们没有登录,则将入口点uri保存在cookie中并重定向到登录页面。

  // application / core / My_Controller.php 
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent :: __ construct
$ this-> load-> library('session');
$ this-> load-> model('tank_auth');
if(!$ this-> tank_auth-> is_logged_in()){
//保存访问者入口点并重定向到登录
$ this-> session-> set_userdata ('redirect',$ this-> uri-> uri_string());
redirect('auth / login');
}
}
}

您的主控制器将扩展 MY_Controller ,不需要担心身份验证。

  MY_Controller 
{
public function index()
{
$ data ['user_id'] = $ this-> tank_auth-> get_user_id();
$ data ['username'] = $ this-> tank_auth-> get_username();
$ this-> load-> view('welcome',$ data);
}
}

您的验证控制器不会扩展MY_Controller,否则它将

 类Auth扩展CI_Controller 
{
public function login b $ b {
$ this-> load-> library('session');
if(auth_success){
//将用户重定向回入口点或欢迎控制器
if($ uri = $ this-> session-> userdata('redirect' )){
redirect($ uri);
} else {
redirect('welcome');
}
}
//句柄认证失败
}
}

而不是使用会话来存储重定向uri,您也可以将它作为 GET 参数传递。


My question is a little hard to explain, but I'll try..

Basically, in the tank_auth example script there is this code to redirect the user if they are not already logged on;

if (!$this->tank_auth->is_logged_in()) {
        redirect('/auth/login/');
    } else {
        $data['user_id']    = $this->tank_auth->get_user_id();
        $data['username']   = $this->tank_auth->get_username();
        $this->load->view('welcome', $data);
    }

Which is great if you have a login page and the user starts at the beginning each time. (And I'm comfortable doing it that way)

But I want the user to be able to jump in to website at (almost) any controller and have a login bar come up across the top. When logging in, it should not redirect them to another page. They should end up on the same page they tried to visit.

For example, my user might load straight away example.com/food/burgers. I'd like a blank page to come up, but just with a login bar across the top. Then when they log in, they end up back on the 'burgers' page but this time there is also a list of burgers and the bar accross the top that tells them they are logged in, with the option to log-off.

So how do I do this? Do I need to call the auth/login method from every controller? Do I do it as an "include"? No idea.

解决方案

Firstly you will want to create a base controller that all your controllers will extend from. You would check for authentication in this base controller. If they aren't logged in, then save the entry point uri in a cookie and redirect to the login page.

// application/core/My_Controller.php
class MY_Controller extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library('session');
        $this->load->model('tank_auth');
        if (!$this->tank_auth->is_logged_in()) {
            // save the visitors entry point and redirect to login
            $this->session->set_userdata('redirect', $this->uri->uri_string());
            redirect('auth/login');
        }
    }
}

Your main controllers will extend MY_Controller and don't need to worry about authentication.

class Welcome extends MY_Controller
{
    public function index()
    {
        $data['user_id']    = $this->tank_auth->get_user_id();
        $data['username']   = $this->tank_auth->get_username();
        $this->load->view('welcome', $data);
    }
}

Your authentication controller wouldn't extend MY_Controller otherwise it will get stuck in a redirect loop.

class Auth extends CI_Controller
{
    public function login()
    {
        $this->load->library('session');
        if (auth_success) {
             // redirect the user back to the entry point or the welcome controller
             if ($uri = $this->session->userdata('redirect')) {
                 redirect($uri);
             } else {
                 redirect('welcome');
             }
        }
        // handle authentication failure
    }
}

Instead of using sessions to store the redirect uri, you could also pass it along as a GET parameter.

这篇关于在CodeIgniter的每个页面上保留Tank Auth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆