php - codeigniter 登录将太多重定向到家庭 Controller

标签 php codeigniter

我的登录和 View 有一个问题,我希望一旦用户登录并退出 session 然后将我重定向到我的家,但是用户将 url 滑到根项目或登录 url 然后它应该保持打开状态home ., 我的代码是这样的

我的 Controller

protected function isLogged(){
        if (!$this->session->userdata('log'))
            redirect('login');
    }

登录

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends MY_Controller {
    public function __construct(){
        parent::__construct();
    }

    public function index(){
        $data['module'] = 'Login';
        $this->load->view('header',$data);
        $this->load->view('login');
    }

    public function getAccess(){
        $username = $this->input->post('username', TRUE);
        $password = $this->input->post('password', TRUE);
        $result = $this->user->login($username,$password);
        if (!empty($this->input->post('username')) && !empty($this->input->post('password'))) {
            if (!$result) {
                $this->json(array('error' => 'invalid username or password'));
            }else{
                $data_session = array(
                    'id' => $result['id'],
                    'first_name' => $result['first_name'],
                    'last_name' => $result['last_name'],
                    'type' => $result['profile_id'],
                    'logged_in' => TRUE 
                );
                $this->session->set_userdata('log',$data_session);
            }
        } else {
            $this->json(array('empty' => 'You did not fill out the required fields.'));
        }
    } 

    public function logout(){
        $this->session->sess_destroy();
        redirect('login','refresh');
    }
}

家庭 Controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends MY_Controller {

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

    public function index(){
        $data = $this->session->userdata('log');
        $data['module']  = "Home";
        $data['fields']  = $this->getModules();
        $this->load->view('header',$data);  
        $this->load->view('index');
        $this->load->view('home');
        $this->load->view('footer');
    }
}

/* End of file Welcome.php */
/* Location: ./application/controllers/Welcome.php */

最佳答案

你可以试试

$this->login();

redirect(base_url());

上次我通过在帮助文件中创建 islogged() 函数解决了同样的问题。

关于php - codeigniter 登录将太多重定向到家庭 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45539162/

相关文章:

一种特殊 HTML 元素的 PHP 爬虫

php - 为每个用户累积值

php - 将变量从 Ajax 传递到 Php

php - 一切,但 [ 和 ] 之间的一切

php - 我的用户声称我的应用程序突然假设了另一个用户

php - 想要在数据库中存储多个文件路径

php - Codeigniter + CSS 不能正常工作,可能是 baseurl

php - SQL - 如何获取创建的同一日期的最新数据

php - 将两个 href 显示到表中的单独列中

php - 在 CodeIgniter 中扩展 Controller 类