在codeigniter中登录时,重定向功能不起作用

问题描述 投票:0回答:2

控制器:

public function home()
{
    $data['admin_id'] = $this->session->userdata('admin_id');
    print_r($data['admin_id']);
}

视图:

<?php
    if($this->input->post('submit'))
    { 
        $username = $this->input->post('username');
        $password = $this->input->post('password');

        $this->db->select('username,password');
        $this->db->from('admin');
        $where = "username='$username' and password = '$password'";
        $this->db->where($where);
        $query = $this->db->get();
        $result = $query->result_array();
        $num = $query->num_rows();
        if($num > 0)
        {
            $this->db->select('username,password,admin_id');
            $this->db->from('admin');
            $where = "username='$username' and password = '$password'";
            $this->db->where($where);
            $query = $this->db->get();
            $result = $query->result_array();
            $this->session->set_userdata('admin_id',$result);
            if($result == true)
            {
                redirect('home');
            }
        }
        else
        {
            echo "<p style='color: red;font-weight: 400;margin-right: 60px;'>Wrong email id or password! </p>";
        }
    }
?>
<form class="form-signin" method="post">
    <input type="text" name="username" id="username" class="form-control" placeholder="username" required autofocus>
    <input type="password" name="password" id="password" class="form-control" placeholder="Password" required>
    <input type="submit" id="submit" name="submit" class="btn btn-lg btn-primary btn-block" value="Sign In">
</form>

在这段代码中,我创建了一个简单的登录页面,我必须使用重定向功能,即重定向('home')。它不工作我不知道为什么但它工作在localhost和控制器内部我也有会话ID即admin_id我也使用print_r函数来打印admin_id的值,但它还没有工作。那么,我该如何解决这个问题呢?请帮我。

谢谢

php codeigniter
2个回答
0
投票

试试这段代码:

请检查autoload.php文件

$autoload['helper'] = array('url');

0
投票

我可以尝试:

<?php
if($this->input->post('submit'))
{ 
    $username = $this->input->post('username',true);
    $password = $this->input->post('password',true);
    $q = $this->db->query("select username,password,admin_id from admin where username ? and password = ?",$array($username,$password));
    if($q->num_rows() > 0){
        $out = $q->result();
        $out = $out[0]
    } else {
        //Error here
        $out = FALSE;
    }
    if($out !== FALSE){
        $this->session->set_userdata('admin_id',$out->admin_id);
            redirect(base_url('home'));
            die();
    }
    else
    {
        echo "<p style='color: red;font-weight: 400;margin-right: 60px;'>Wrong email id or password! </p>";
    }
}

?>

并在application / config / autoload =>在助手数组中插入url。 $autoload['helper'] = array('url');

在你的config.php你可以设置$ config ['base_url']

© www.soinside.com 2019 - 2024. All rights reserved.