codeigniter ajax登录错误div和会话问题

时间:2015-06-29 17:19:30

标签: javascript php jquery ajax codeigniter

我使用带有AppUI 2.1 bootstrap主题的codeigniter 3.0创建了一个登录,它总是在登录时显示错误div,每次我成功登录时,它也会成功导向到Maps视图,但是当我导航到Information Table视图时返回地图视图,它将重定向到登录视图。

有什么建议吗?这是我的代码:

模型/ Login_model.php

class Login_model extends CI_Model {

public function login($username, $password) {
    $passwordEncrypt = sha1($password);
    $this->db->where('username', $username);
    $this->db->where('password', $passwordEncrypt);
    $query = $this->db->get('admin');
    if ($query->num_rows() == 1) {
        foreach ($query->result() as $row) {
            $data = array(
                'username' => $row->username,
                'password' => $row->password,
                'logged_in' => true
            );
        }
        $this->load->library('session');
        $this->session->set_userdata($data);
        return true;
    } else {
        return false;
    }
}

public function isLoggedIn() {
    header("cache-Control: no-store, no-cache, must-revalidate");
    header("cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");
    $is_logged_in = $this->session->userdata('logged_in');

    if (!isset($is_logged_in) || $is_logged_in !== true) {
        redirect('/');
        exit;
    }
}

}

视图/ page_ready_login.php

<?php
include 'assets/Backend/inc/config.php';
$template['title'] = 'BCGIS | LOGIN';
?>
<?php include 'assets/Backend/inc/template_start.php'; ?>

<!-- Login Container -->
<div id="login-container">
    <!-- Login Header -->
    <h1 class="h3 text-light text-center push-top-bottom fadeIn animated">
        <div class="row">
            <div class="col-lg-3 fadeIn animated">
                <img src="<?= base_url(); ?>assets/Backend/images/Ph_seal_davao_del_norte_panabo_city.png" style="width: 95px; height: 80px;"/>
            </div>
            <div class="col-lg-9 fadeIn animated">
                <strong>Brgy. Cagangohan Geographical Information System</strong>
            </div>
        </div>
    </h1>
    <!-- END Login Header -->

    <!-- Login Block -->
    <div class="block fadeIn animated">
        <!-- Login Title -->
        <div class="block-title">
            <h2>Administration Login</h2>
        </div>
        <!-- END Login Title -->

        <!-- Login Form -->
        <form id="form-login" action="<?= base_url(); ?>Maps" method="post" class="form-horizontal">
            <p class="alert alert-danger" id="response"><b>Invalid Administrator's Username or Password.</b></p>
            <div class="form-group">
                <div class="col-xs-12">
                    <input type="text" id="login-username" name="login-username" class="form-control" placeholder="Username..">
                </div>
            </div>
            <div class="form-group">
                <div class="col-xs-12">
                    <input type="password" id="login-password" name="login-password" class="form-control" placeholder="Password..">
                </div>
            </div>
            <div class="form-group form-actions">
                <div class="col-sm-offset-2 col-sm-8 text-center">
                    <center>
                        <button type="submit" class="btn btn-effect-ripple btn-block btn-primary"><i class="fa fa-check"></i> Sign In</button>
                    </center>
                </div>
            </div>
        </form>
        <!-- END Login Form -->
    </div>
    <!-- END Login Block -->

    <!-- Footer -->
    <footer class="text-muted text-center fadeIn animated">
        <small><span id="year-copy"></span> &copy; <a href="#"><?php echo $template['name'] . ' ' . $template['version']; ?></a></small>
    </footer>
    <!-- END Footer -->
</div>
<!-- END Login Container -->

<?php include 'assets/Backend/inc/template_scripts.php'; ?>

<!-- Load and execute javascript code used only in this page -->
<script src="<?= base_url(); ?>assets/Backend/js/pages/readyLogin.js"></script>
<script>
    $(function () {
        ReadyLogin.init();
    });
</script>

<?php include 'assets/Backend/inc/template_end.php'; ?>

控制器/ login.php中

class Login extends CI_Controller {

public function __construct() {
    parent::__construct();
    $this->load->library('session');
    $this->load->helper('form');
    $this->load->helper('url');
    $this->load->helper('html');
    $this->load->library('form_validation');
    $this->load->Model("Login_model");
}

public function index() {
    $this->load->view('Backend/page_ready_login');
}

public function logout() {
    $this->session->sess_destroy();
    redirect(base_url(), 'refresh');
}

public function login() {

}

controller / Maps.php(成功登录时可以指示)

class Maps extends CI_Controller {

public function __construct() {
    parent::__construct();
    $this->load->Model('Login_model');
}

public function index() {
    $username = $this->input->post('login-username');
    $password = $this->input->post('login-password');

    if ($this->Login_model->login($username, $password)) {
        $this->Login_model->isLoggedIn();
        $this->load->view('Backend/page_comp_maps');
    } else {
        redirect(base_url(), 'refresh');
    }
}

assets / Backend / js / pages / readyLogin.js

var ReadyLogin = function () {

return {
    init: function () {
        /*
         *  Jquery Validation, Check out more examples and documentation at https://github.com/jzaefferer/jquery-validation
         */

        /* Login form - Initialize Validation */
        $('#form-login').validate({
            errorClass: 'help-block shake animated', // You can change the animation class for a different entrance animation - check animations page
            errorElement: 'div',
            errorPlacement: function (error, e) {
                e.parents('.form-group > div').append(error);
            },
            highlight: function (e) {
                $(e).closest('.form-group').removeClass('has-success has-error').addClass('has-error');
                $(e).closest('.help-block').remove();
            },
            success: function (e) {
                e.closest('.form-group').removeClass('has-success has-error');
                e.closest('.help-block').remove();
            },
            rules: {
                'login-username': {
                    required: true,
                    error: true
                },
                'login-password': {
                    required: true,
                    minlength: 5,
                    error: true
                }
            },
            messages: {
                'login-username': {
                    required: "Please enter Administrator's username.",
                    error: "Invalid Administrator's username."
                },
                'login-password': {
                    required: "Please provide Administrator's password.",
                    minlength: "Admin's password must be at least 5 characters long.",
                    error: "Invalid Administrator's password."
                }
            }
        });
        $("#form-login").submit(function (e){
            e.preventDefault();
            var url = $(this).attr('action');
            var method = $(this).attr('method');
            var data = $(this).serialize();

            $.ajax({
               url:url,
               type:method,
               data:data
            }).done(function(data){
               if(data !=='')
                {
                    $("#response").show('fast');
                    $("#response").effect( "shake" );
                    $('#form-login')[0].reset();
                }
                else
                {
                window.location.href = url;
                throw new Error('go');
                } 
            });
        });

        $( "div" ).each(function( index ) {
        var cl = $(this).attr('class');
        if(cl ==='')
            {
                $(this).hide();
            }
        });
    }
};

}();

在$('#form-login')。submit(function(e){})之间的readyLogin.js中,似乎其中的代码不起作用。

了解更多信息,此处为我project的链接。

任何帮助都会非常感激。 :)

0 个答案:

没有答案