【问题标题】:Codeigniter: Redirect not workingCodeigniter:重定向不起作用
【发布时间】:2015-02-22 15:00:53
【问题描述】:

我已经在这里搜索了一些其他类似问题的帖子,但我还没有设法完成这项工作。不知道是什么问题。

  • Login 控制器登录/注销后,我正尝试重定向到主Home 控制器

我在我的开发环境变量中设置了error_reporting(E_ALL) 并且没有错误并尝试在我的Login 控制器中加载url 帮助程序:$this->load->helper('url');

这里是主要的Home 控制器:

public function index() {
        if (($this->session->userdata('username') != "")) {
            $this->user();
        } else {
            $this->load->view('login');
        }
    }

这是Login 控制器:

public function index()
    {
        $this->load->view('login');
    }
    public function logout()
    {
                $profile = array(
                    'userid' => '',
                    'username' => '',
                    'role' => '',
                    'custid' => '',
                    'custname' => '',
                    'logged_in' => FALSE 
                    );

        $this->session->unset_userdata($profile);
        $this->session->sess_destroy();
        redirect('/', 'refresh');
    }
    public function logon()
    {
        $data = $_POST;
        $this->db->select("people.*, customers.name as 'custname', customers.id as 'custid'");
        $this->db->from('people');
        $this->db->join('customers', 'customers.id = people.customer');
        $this->db->where('people.username', $data['username']);
        $this->db->where('people.password', $data['password']);
        $query = $this->db->get();
        $results = $query->result_array();

        if(count($results) > 0)
        {
            $profile = array(
               'username'  => $results[0]['username'],
               'userid'  => $results[0]['id'],
               'role'  => $results[0]['role'],
               'date_format'  => $results[0]['date_format'],
               'custname' => $results[0]['custname'],
               'custid' => $results[0]['custid'],
               'logged_in' => TRUE
            );

            $this->db->select("*");
            $this->db->from('hub.settings');
            $this->db->where('customer', $profile['custid']);
            $query = $this->db->get();
            $results = $query->result_array();
            if(count($results) > 0)
            {
                $profile['contacthtml'] = $results[0]['contacthtml'];
                $profile['accentcolor'] = $results[0]['accentcolor'];
            }

            $this->session->set_userdata($profile);
            redirect('/', 'refresh');
        }else{
            redirect('/login', 'refresh');
        }
    }

我得到的只是网址 http://localhost:85/login/logon 和一个空白屏幕。

我也试过:redirect('/home', 'refresh');redirect('/home');redirect('home', 'refresh');redirect('home/', 'refresh');redirect('/home/', 'refresh');

编辑:

这是来自开发工具中网络部分的响应:

【问题讨论】:

  • 你确定count($results)> 0 吗?因为redirect("home") 应该足够了。 (附带说明,您的控制器中不应该有查询,它们属于模型)
  • 是的,我刚刚检查了 if 中的 &results 变量,它有值(在您的旁注中,为了简单起见)
  • 如果你放置die("hello");而不是你的redirect("home"),你看到你好了吗?
  • 你忘记了斜杠:$config['base_url'] = 'http://localhost:85/';
  • @AdrienXL:是的,谢谢 AdrienXL,解决了它,你应该发布答案,以便我接受。

标签: php codeigniter redirect


【解决方案1】:

正如 cmets 中所说,配置文件中的 base_url 必须使用尾部斜杠设置。在你的情况下:

$config['base_url'] = 'http://localhost:85/';

【讨论】:

    【解决方案2】:

    这可能是由于 Chrome 重定向问题,如http://forums.asp.net/t/1599598.aspx?Response+Redirect+problem+in+Google+Chrome 所述。 您应该尝试其他浏览器来确定。如果是这样,那么您应该清除 Chrome 缓存。

    【讨论】:

      猜你喜欢
      • 2018-12-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-15
      • 2015-02-28
      • 2013-08-13
      • 1970-01-01
      • 2016-02-14
      • 2020-03-01
      相关资源
      最近更新 更多