【问题标题】:Codeigniter - Controller can't render viewCodeigniter - 控制器无法呈现视图
【发布时间】:2016-10-27 17:41:03
【问题描述】:

我在尝试在我的 login.php 控制器中呈现 login_view.php 时遇到问题。我得到的是这个错误:

Firefox 检测到服务器正在重定向请求 这个地址永远不会完成。

此问题有时可能是由禁用或拒绝接受 cookie 引起的。

这是我的控制器

class Login extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form','url','html'));
        $this->load->library(array('session','form_validation'));
        $this->load->database();
        $this->load->model('user_model');
    }

    function index()
    {
        //get form input
        $email = $this->input->post('email');
        $password = $this->input->post('password');

        //form validation
        $this->form_validation->set_rules('email','Email-ID','trim|required|xss_clean');
        $this->form_validation->set_rules('password','Password','trim|required|xss_clean');

        if($this->form_validation->run() == FLASE)
        {
            //validation fail
            $this->load->view('login_view');
        }
        else
        {
            //check user credentials
            $uresult = $this->user_model->get_user($email, $password);
            if(count($uresult)>0)
            {
                //set session
                $sess_data = array('login' => TRUE, 'uname' => $uresult[0]->fname,'uid' => $uresult[0]->id);
                $this->session->set_userdata($sess_data);
                redirect('profile/index');
            }
            else
            {
                $this->session->set_flashdata('msg','<div class = "alert alert-danger text-center">Wrong Email/Password</div>');
                redirect('login/index');
            }
        }
    }
}

请帮我找出问题所在。谢谢。

【问题讨论】:

  • 你在 chrome 上检查了吗?谷歌浏览器怎么说?
  • @Nandan 本地主机页面不工作。本地主机重定向您的次数过多。
  • 是“localhost/project_name/Login/index”吗??
  • 我认为我的控制器中的条件逻辑有问题,但我不确定在哪里
  • 是的,它是 localhost/project_name/Login/index。有什么问题吗?

标签: php codeigniter session


【解决方案1】:

我建议您在自动加载中加载会话库,因为您将在其他页面上需要它。此外,您在第一个 if 条件中有错字。

if($this->form_validation->run() == FLASE) // FIX FALSE

【讨论】:

  • 谢谢。刚刚注意到。
【解决方案2】:

修好了!原来我错误地使用了redirect 而不是$this-&gt;load-&gt;view('login_view'); 和$this-&gt;load-&gt;view('profile_view');

似乎如果我使用redirect 并且代码将始终检查if(count($uresult)&gt;0)TRUE 还是FALSE 然后继续操作,redirect 操作将始终运行。所以这是无限循环。

查看检查用户凭据条件语句。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-13
    • 2011-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 2014-11-20
    • 1970-01-01
    相关资源
    最近更新 更多