codeigniter登录页面不会重定向到仪表板

时间:2018-02-07 20:34:06

标签: php codeigniter login dashboard

我在名为“pages.php”的控制器文件中出现问题。通过具有正确凭据的登录页面登录后,pages.php无法打开仪表板。我怀疑这个问题的来源可能是在 pages.php

中找到的
<?php 
class Pages extends MY_Controller
{
public function view($page = 'login')
{
    if (!file_exists(APPPATH.'views/'.$page.'.php'))
    {
        // Whoops, we don't have a page for that!
        show_404();
    }

    $data['title'] = ucfirst($page); // Capitalize the first letter

    if($page == 'section' || $page == 'subject' || $page == 'student' || $page == 'marksheet' || $page == 'accounting') {
        $this->load->model('model_classes');
        $data['classData'] = $this->model_classes->fetchClassData();

        $this->load->model('model_teacher');
        $data['teacherData'] = $this->model_teacher->fetchTeacherData();


        $this->load->model('model_accounting');
        $data['totalIncome'] = $this->model_accounting->totalIncome();
        $data['totalExpenses'] = $this->model_accounting->totalExpenses();
        $data['totalBudget'] = $this->model_accounting->totalBudget();
    }

    if($page == 'setting') {
        $this->load->model('model_users');
        $this->load->library('session');
        $userId = $this->session->userdata('id');
        $data['userData'] = $this->model_users->fetchUserData($userId);
    }

    if($page == 'dashboard') {
        $this->load->model('model_student');
        $this->load->model('model_teacher');
        $this->load->model('model_classes');
        $this->load->model('model_marksheet');
        $this->load->model('model_accounting');

        $data['countTotalStudent'] = $this->model_student->countTotalStudent();
        $data['countTotalTeacher'] = $this->model_teacher->countTotalTeacher();
        $data['countTotalClasses'] = $this->model_classes->countTotalClass();
        $data['countTotalMarksheet'] = $this->model_marksheet->countTotalMarksheet();

        $data['totalIncome'] = $this->model_accounting->totalIncome();
        $data['totalExpenses'] = $this->model_accounting->totalExpenses();
        $data['totalBudget'] = $this->model_accounting->totalBudget();
    }

    if($page == 'login') {
        $this->isLoggedIn();
        $this->load->view($page, $data);
    } 
    else{
        $this->isNotLoggedIn();

        $this->load->view('templates/header', $data);
        $this->load->view($page, $data);    
        $this->load->view('templates/footer', $data);    
    }
}

}

任何可以编辑此代码的人都可以提供帮助。或者,如果您认为问题不在 pages.php (上页)中,请告诉我如何解决问题。我已经提到了更多页面: -

这是 rootes.php。

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';


$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

核心文件夹

中的 MY_Controller.php
<?php 
class MY_Controller extends CI_Controller 
{
public function __construct()
{
    parent::__construct();
}

public function isLoggedIn()
{

    $this->load->library('session');

    if($this->session->userdata('logged_in') === true) {
        redirect('../dashboard');
    }
}   

public function isNotLoggedIn()
{

    $this->load->library('session');

    if($this->session->userdata('logged_in') != true) {
        redirect('../../');
    }
}

}

这是控制器文件夹

中的 Users.php
<?php 
class Users extends MY_Controller
{
public function __construct()
{
    parent::__construct();

    // loading the users model
    $this->load->model('model_users');

    // loading the form validation library
    $this->load->library('form_validation');        

}

public function login()
{

    $validator = array('success' => false, 'messages' => array());

    $validate_data = array(
        array(
            'field' => 'username',
            'label' => 'Username',
            'rules' => 'required|callback_validate_username'
        ),
        array(
            'field' => 'password',
            'label' => 'Password',
            'rules' => 'required'
        )
    );

    $this->form_validation->set_rules($validate_data);
    $this->form_validation->set_error_delimiters('<p class="text-danger">','</p>');

    if($this->form_validation->run() === true) {            
        $username = $this->input->post('username');
        $password = md5($this->input->post('password'));

        $login = $this->model_users->login($username, $password);

        if($login) {
            $this->load->library('session');

            $user_data = array(
                'id' => $login,
                'logged_in' => true
            );

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

            $validator['success'] = true;
            $validator['messages'] = "index.php/dashboard";             
        }   
        else {
            $validator['success'] = false;
            $validator['messages'] = "Incorrect username/password combination";
        } // /else

    }   
    else {
        $validator['success'] = false;
        foreach ($_POST as $key => $value) {
            $validator['messages'][$key] = form_error($key);
        }           
    } // /else

    echo json_encode($validator);
} // /lgoin function

public function validate_username()
{
    $validate = $this->model_users->validate_username($this->input->post('username'));

    if($validate === true) {
        return true;
    } 
    else {
        $this->form_validation->set_message('validate_username', 'The {field} does not exists');
        return false;           
    } // /else
} // /validate username function

public function logout()
{
    $this->load->library('session');
    $this->session->sess_destroy();
    redirect('../../');
}


public function updateProfile()
{
    $this->load->library('session');
    $userId = $this->session->userdata('id');

    $validator = array('success' => false, 'messages' => array());

    $validate_data = array(
        array(
            'field' => 'username',
            'label' => 'Username',
            'rules' => 'required'
        ),
        array(
            'field' => 'fname',
            'label' => 'First Name',
            'rules' => 'required'
        )
    );

    $this->form_validation->set_rules($validate_data);
    $this->form_validation->set_error_delimiters('<p class="text-danger">','</p>');

    if($this->form_validation->run() === true) {    
        $update = $this->model_users->updateProfile($userId);                   
        if($update === true) {
            $validator['success'] = true;
            $validator['messages'] = "Successfully Update";
        }
        else {
            $validator['success'] = false;
            $validator['messages'] = "Error while inserting the information into the database";
        }           
    }   
    else {
        $validator['success'] = false;
        foreach ($_POST as $key => $value) {
            $validator['messages'][$key] = form_error($key);
        }           
    } // /else

    echo json_encode($validator);
}

public function changePassword()
{
    $this->load->library('session');
    $userId = $this->session->userdata('id');

    $validator = array('success' => false, 'messages' => array());

    $validate_data = array(
        array(
            'field' => 'currentPassword',
            'label' => 'Current Password',
            'rules' => 'required|callback_validate_current_password'
        ),
        array(
            'field' => 'newPassword',
            'label' => 'Password',
            'rules' => 'required|matches[confirmPassword]'
        ),
        array(
            'field' => 'confirmPassword',
            'label' => 'Confirm Password',
            'rules' => 'required'
        )
    );

    $this->form_validation->set_rules($validate_data);
    $this->form_validation->set_error_delimiters('<p class="text-danger">','</p>');     

    if($this->form_validation->run() === true) {    
        $update = $this->model_users->changePassword($userId);                  
        if($update === true) {
            $validator['success'] = true;
            $validator['messages'] = "Successfully Update";
        }
        else {
            $validator['success'] = false;
            $validator['messages'] = "Error while inserting the information into the database";
        }           
    }   
    else {
        $validator['success'] = false;
        foreach ($_POST as $key => $value) {
            $validator['messages'][$key] = form_error($key);
        }           
    } // /else

    echo json_encode($validator);
}

public function validate_current_password()
{
    $this->load->library('session');
    $userId = $this->session->userdata('id');
    $validate = $this->model_users->validate_current_password($this->input->post('currentPassword'), $userId);

    if($validate === true) {
        return true;
    } 
    else {
        $this->form_validation->set_message('validate_current_password', 'The {field} is incorrect');
        return false;           
    } // /else  
}

}

这是视图文件夹

中的 login.php
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title; ?></title>

<!-- bootstrap css -->
<link rel="stylesheet" type="text/css" 
href="assets/bootstrap/css/bootstrap.min.css">
<!-- boostrap theme -->
<link rel="stylesheet" type="text/css" href="assets/bootstrap/css/bootstrap-theme.min.css">

<!-- custom css -->
<link rel="stylesheet" type="text/css" href="custom/css/custom.css">    

<!-- jquery -->
<script type="text/javascript" src="assets/jquery/jquery.min.js"></script>
<!-- boostrap js -->
<script type="text/javascript" src="assets/bootstrap/js/bootstrap.min.js">
</script>

</head>
<body>


<div class="col-md-6 col-md-offset-3 vertical-off-4">
<div class="panel panel-default login-form">
    <div class="panel-body">
        <form method="post" action="index.php/users/login" id="loginForm">
            <fieldset>
                <legend>
                    Login
                </legend>

                <div id="message"></div>

                <div class="form-group">
                    <label for="username">Username</label>
                    <input type="text" class="form-control" id="username" name="username" placeholder="Username" autofocus>
                </div>
                <div class="form-group">
                    <label for="password">Password</label>
                    <input type="password" class="form-control" id="password" name="password" placeholder="Password">
                </div>                                           

                <button type="submit" class="col-md-12 btn btn-primary login-button">Submit</button>                    
            </fieldset>
        </form>
    </div>
</div>
</div>

<script type="text/javascript" src="custom/js/login.js"></script>
</body>
</html>

login.js

$(document).ready(function(){
$("#loginForm").unbind('submit').bind('submit', function() {

    var form = $(this);
    var url = form.attr('action');
    var type = form.attr('method');

    $.ajax({
        url  : url,
        type : type,
        data : form.serialize(),
        dataType: 'json',
        success:function(response) {
            if(response.success === true) {
                window.location = response.messages;
            }
            else {
                if(response.messages instanceof Object) {
                    $("#message").html('');     

                    $.each(response.messages, function(index, value) {
                        var key = $("#" + index);

                        key.closest('.form-group')
                        .removeClass('has-error')
                        .removeClass('has-success')
                        .addClass(value.length > 0 ? 'has-error' : 'has-success')
                        .find('.text-danger').remove();                         

                        key.after(value);

                    });
                } 
                else {                      
                    $(".text-danger").remove();
                    $(".form-group").removeClass('has-error').removeClass('has-success');

                    $("#message").html('<div class="alert alert-warning alert-dismissible" role="alert">'+
                      '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>'+
                      response.messages + 
                    '</div>');
                } // /else
            } // /else
        } // /if
    });

    return false;
});
});

3 个答案:

答案 0 :(得分:0)

感谢所有人,最后我得到了正确答案,问题是由于php 7服务器中的旧版codeignitor,有关详细信息,请点击

Codeigniter session data lost after redirect

答案 1 :(得分:0)

嗨,我面临着同样的问题。日志什么也没显示,我的Apache服务器被挂起。但就我而言,问题出在我的控制器上。我犯了一个错误。我正在日志中打印数组对象。而不是进行数组到字符串的转换。我解决了这个问题,重定向功能开始起作用。

答案 2 :(得分:0)

只需使用刷新方法,它对我有用

style="background-image:url({% static 'images/intro.png' %})"></div>