dongmangsha7354 2014-01-26 23:37
浏览 17
已采纳

重定向到登录页面 - Codeigniter

I want to check the session; if it exits it goes to my web site, otherwise redirecting to "login" page; here is my controller:

function __construct() {
    parent::__construct();
    $this->is_logged_in();
}

public function index($failed=FALSE) {
    $data['main_content'] = 'login_view';
    $data['failed_validation'] = $failed;
    $this->load->view('includes/template', $data);
}

public function is_logged_in() {
    if ($this->session->userdata('f_name') != null) {
        return;
    } else {
        $this->index();
       //die()// This one does not help either
    }
}

public function display_site() {
    $data['main_content'] = 'site_view';
    $this->load->view('includes/template', $data);
}

This is the version after suggestion(but still the same problem!):

Controller:

function __construct() {
    parent::__construct();
    $this->load->model('user_model');
    if ($this->user_model->is_logged_in() == FALSE) {
        $this->index();
    }
}

public function index($failed=FALSE) {
    $data['main_content'] = 'login_view';
    $data['failed_validation'] = $failed;
    $this->load->view('includes/template', $data);
}

public function login() {
    $data['main_content'] = 'login_view';
    $data['failed_validation'] = FALSE;
    $this->load->view('includes/template', $data);
}

Model:

  public function is_logged_in()
    {
         return ($this->session->userdata('is_logged_in') );
    }

Thats fine but when the user goes to logs in page, it shows both login page and my website main page! In other hand even if I die in (see the comment in my code) it does not help, it gives me a blank page!

Thanks

  • 写回答

2条回答 默认 最新

  • dongzhui4927 2014-01-27 00:08
    关注

    Login Controller:

    class Login extends CI_Controller {
    
        public function __construct() {
            parent::__construct();
        }
    
        public function index() {
            $this->load->view('includes/login');
        }
    }
    

    Main Controller:

    class Main extends CI_Controller {
        public function __construct() {
            parent::__construct();      
                $this->load->model('user_model');
                if (!$this->user_model->is_logged_in()) {
                   redirect(base_url().'login', 'location', 301);
                }   
        }
    
        public function index() {
            $this->load->view('includes/main');
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?