用户重定向不工作Codeigniter

时间:2014-08-29 10:29:41

标签: php codeigniter

在我的用户库中,我有一个名为 login

的功能

我现在遇到的问题是因为我在仪表板控制器上放了一个重定向功能。它阻止我登录。

我所追求的是,​​如果没有登录会阻止/重定向用户到管理员登录。

我已自动加载了我的用户库。

class Dashboard extends MX_Controller {

    public function __construct()
   {
      parent::__construct();
      $this->lang->load('admin/common/dashboard', 'english');

      if($this->user->login() == FALSE) {
        redirect("admin");
        }
   }

}

登录控制器

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

class Login extends MX_Controller {
    private $error = array();

    public function __construct()
   {
      parent::__construct();
      $this->lang->load('admin/common/login', 'english');
   }

    public function index()
    {

        $this->document->setTitle($this->lang->line('heading_title'));

        if (($this->input->server('REQUEST_METHOD') == 'POST') && $this->validate()) {

            $data_session = array(
                'user_id' => $this->user->isLogged()
            );

            $this->session->set_userdata($data_session);

            redirect('admin/dashboard');
        }

        $data['heading_title'] = $this->lang->line('heading_title');

        $data['text_login'] = $this->lang->line('text_login');
        $data['text_forgotten'] = $this->lang->line('text_forgotten');

        $data['entry_username'] = $this->lang->line('entry_username');
        $data['entry_password'] = $this->lang->line('entry_password');

        $data['button_login'] = $this->lang->line('button_login');

        if (array_key_exists('warning', $this->error)) {
            $data['error_warning'] = $this->error['warning'];
        } else {
            $data['error_warning'] = '';
        }

        $data['action'] = site_url('admin');

        if (trim($this->input->post('username'))) {
            $data['username'] = $this->input->post('username');
        } else {
            $data['username'] = '';
        }

        if (trim($this->input->post('password'))) {
            $data['password'] = $this->input->post('password');
        } else {
            $data['password'] = '';
        }


        return $this->load->view('common/login', $data);
    }

    protected function validate() {
        if (!trim($this->input->post('username')) || !trim($this->input->post('password')) || !$this->user->login($this->input->post('username'), $this->input->post('password'))) {
            $this->error['warning'] = $this->lang->line('error_login');
        }

        return !$this->error;
    }
}

用户图书馆

<?php
class User {
    private $user_id;
    private $username;
    private $permission = array();

    public function __construct() {
        $this->CI =& get_instance();

        if (trim($this->CI->session->userdata('user_id'))) {
            $user_query = $this->CI->db->query("SELECT * FROM " . $this->CI->db->dbprefix . "user WHERE user_id = '" . (int)$this->CI->session->userdata('user_id') . "' AND status = '1'");

            if ($user_query->num_rows) {
                $this->user_id = $user_query->row('user_id');
                $this->username = $user_query->row('username');
                $this->last_logged = $user_query->row('last_logged');

                $this->CI->db->query("UPDATE " . $this->CI->db->dbprefix . "user SET ip = " . $this->CI->db->escape($this->CI->input->ip_address()) . " WHERE user_id = '" . (int)$this->CI->session->userdata('user_id') . "'");

                $now = date("Y-m-d H:i:s");

                $data = array(
               'last_logged' => $now
            );

                $this->CI->db->where('user_id', $this->CI->session->userdata('user_id'));
                $this->CI->db->update('user', $data); 
            } else {
                $this->logout();
            }
        }
    }

    public function login($username, $password) {
        $user_query = $this->CI->db->query("SELECT * FROM " . $this->CI->db->dbprefix . "user WHERE username = " . $this->CI->db->escape($username) . " AND (password = SHA1(CONCAT(salt, SHA1(CONCAT(salt, SHA1(" . $this->CI->db->escape($password) . "))))) OR password = " . $this->CI->db->escape(md5($password)) . ") AND status = '1'");

        if ($user_query->num_rows) {

            $this->user_id = $user_query->row('user_id');
            $this->username = $user_query->row('username');

            return true;
        } else {
            return false;
        }
    }

    public function logout() {
        //$this->CI->session->sess_destroy();
        $this->CI->session->unset_userdata($this->CI->session->userdata('user_id'));
        $this->user_id = '';
        $this->username = '';
    }

    public function isLogged() {
        return $this->user_id;
    }

    public function last_logged() {
        return $this->last_logged;
    }

    public function getId() {
        return $this->user_id;
    }

    public function getUserName() {
        return $this->username;
    }
}

2 个答案:

答案 0 :(得分:0)

不是由于重定向中的无限循环?您可以使用:

$this->router->class 

$this->router->method

尝试验证何时应用重定向。

答案 1 :(得分:0)

我在考虑过夜之后。我发现了什么问题。我从用户库中的父构造区域删除了修剪。

用户lib

if (trim($this->CI->session->userdata('user_id'))) {

if ($this->CI->session->userdata('user_id')) {

在我的仪表板控制器上的父构造区域,我放了。

if ($this->session->userdata('isLogged')) {
return true;
} else {
redirect('admin');
}

现在全部工作