方法不重定向到codeigniter中的给定视图

时间:2016-02-25 04:58:07

标签: php codeigniter codeigniter-3

我正在尝试在codeigniter中实现登录。 一切正常,但是当我输入正确的用户名和密码时,它不会重定向到管理页面。 请帮帮我,出了什么问题。

login_control

class login_control extends CI_Controller
{
    public function __construct() {
        parent::__construct();

        $this->load->model('login_model');
    }
    function index()
    {
        $this->load->view('login_view');
    }

    function verify()
    {

        $query = $this->login_model->login();
        $this->load->library('form_validation');
$this->form_validation->set_rules('uname','Username','trim|required|min_length[8]');
$this->form_validation->set_rules('pass','Password','trim|required|max_length[6]');

        if($query)
        {
            $data = array(
                'username' => $this->input->post('uname'),
                'is_logged_in' => true
            );

            $this->session->set_userdata($data);
            redirect('admin_control/admin');
        }
        else 
        {
            $this->index();

            }

    }

login_model

class login_model extends CI_Model
{
    function login()
    {
        $this->db->where('username',  $this->input->post('uname'));
        $this->db->where('password', md5($this->input->post('pass')));

        $sql = $this->db->get("signup");
         if($sql->num_rows() == 1)
            {
                return true;
            }

     }

}

login_view

<body>
<div class="form-wrapper">

  <form action="login_control/verify" method="post">
    <h3>Login here</h3>

    <div class="form-item">
        <input type="text" name="uname" required="required" placeholder="Username" autofocus></input>
    </div>

    <div class="form-item">
        <input type="password" name="pass" required="required" placeholder="Password"></input>
    </div>

    <div class="button-panel">
        <input type="submit" class="button" title="Log In" value="Login"></input>
    </div>

  </form>

  <div class="reminder">
    <p>Not a member? <a href="#">Sign up now</a></p>
    <p><a href="#">Forgot password?</a></p>
  </div>

</div>

</body>

admin_control

<?php

class admin_control extends CI_Controller
{
    public function __construct() {
        parent::__construct();
        $this->is_logged_in();
    }

    function admin()
    {
        $this->load->view('admin_view');
    }

    function is_logged_in()
    {
        $login = $this->session->userdata('is_logged_in');
        if(!isset($login) || $login != TRUE)
        {
            echo 'Sorry, you are not a admin. ';
            die();
        }
    }
}

4 个答案:

答案 0 :(得分:1)

使用此代码

redirect('admin_control/admin',true);

OR

redirect('admin_control/admin','refresh');

如果你不删除index.php

redirect('index.php/admin_control/admin',true);

答案 1 :(得分:0)

在查询数据库时,您在class login_model extends CI_Model { function login() { $this->db->where('username', $this->input->post('uname')); $this->db->where('password', md5($this->input->post('pass'))); $sql = $this->db->get("signup"); if ($sql->num_rows() == 1) { return true; } } } 中犯了一些错误。

而不是:

class login_model extends CI_Model {

    function login() {
        $this->db->select('id, username, password');
        $this->db->from('signup');
        $this->db->where('username', $this->input->post('uname'));
        $this->db->where('password', MD5($this->input->post('pass'));
        $this->db->limit(1);

        $sql = $this->db->get();
        if ($sql->num_rows() == 1) {
            return $query->result();
        } else {
            return false;
        }
    }

}

使用此:

Exception

有关详细信息,请参阅此doc

答案 2 :(得分:0)

试试这个

<强> login_control

public function __construct() 
   {
        parent::__construct();

        $this->load->model('login_model');
    }
    function index()
    {
        $this->load->view('login_view');
    }

    function verify()
    {

        $query = $this->login_model->login();
        $this->load->library('form_validation');
        $this->form_validation->set_rules('uname','Username','trim|required|min_length[8]');
        $this->form_validation->set_rules('pass','Password','trim|required|max_length[6]');

        if($query!= null)
        {
            foreach ($query as $row) 
            {
                $data = array(
                    'username' => $row->username,
                    'is_logged_in' => true
                );
            $this->session->set_userdata($data);
            }
            redirect(base_url('admin_control/admin'),'refresh');
        }
        else 
        {
            $this->index();

        }

<强>模型

class login_model extends CI_Model
{
    function login()
    {
        $pass = md5($this->input->post('pass'));
        $this->db->where('username',  $this->input->post('uname'));
        $this->db->where('password',$pass);

        $sql = $this->db->get("signup");
         if($sql->num_rows() > 0)
            {
                   return $query->result();
            }
           else{
                  return false;

     }

}

答案 3 :(得分:0)

最后我解决了我的问题!!!!!

我要感谢所有参与者回答这个问题以及他们宝贵的时间。

实际上,表中passwd的数据类型是VARCHAR(10)。我只是用VARCHAR(255)替换它,我的问题解决了! ..