不在Codeigniter 3框架中第一次登录帐户

时间:2017-12-21 18:22:12

标签: php codeigniter

我有一个网站和登录系统。在localhost,一切都很完美。但是,在服务器(HOSTGATOR)中,当人们想要登录他们的帐户时,他们会输入登录密码并点击"登录"按钮,它刷新页面但不登录。它们应该输入2-3次然后系统登录。问题出在哪里?

public function __construct()
{
    parent::__construct();
    $this->load->helper(array('form','url', 'security'));
    $this->load->library(array('session', 'form_validation', 'email'));
    $this->load->database();
    $this->load->model('User_model');
}

 public function login(){
            $data['title'] = 'Sign In';

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

        $validate_data = array(
            array(
                'field' => 'usernameforlog',
                'label' => lang('controller-username-label'),
                'rules' => 'trim|required|alpha_dash'
            ),
            array(
                'field' => 'passwordforlog',
                'label' => lang('controller-password-label'),
                'rules' => 'trim|required|md5'
            )
        );

        $this->form_validation->set_rules($validate_data);
        $this->form_validation->set_message('required', lang("form_validation_required"));  
        $this->form_validation->set_message('alpha_dash', lang("form_validation_alpha_dash"));
        $this->form_validation->set_error_delimiters('<p class="text-danger">', '</p>'); 


            if ($this->form_validation->run() === FALSE)
            {

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

                log_message('error', 'validation errors' );

            }else {

                // Get username
                $username = mb_strtolower($this->input->post('usernameforlog'), 'UTF-8');
                // Get and encrypt the password
                $password = $this->input->post('passwordforlog');
                // Login user

                $user_id = $this->user_model->login($username, $password);
                if($user_id){
                    // Create session

                    $user_data = array( 
                    'id' => $user_id, 
                    'instructors_slug' => $username, 
                    'logged_in' => true 
                    );

                    $this->session->set_userdata($user_data);
                    $validator['success'] = true;
                    $validator['messages'] = array();
                } else {

                    $validator['success'] = false;
                    $validator['messages'] = '<div class="alert alert-danger text-center">'.lang('controller-user-email-error3').'</div>';
                }       
            }

            echo json_encode($validator);
        }

这是ajax文件:

$(document).ready(function() {
    $("#loginform").unbind('submit').bind('submit', function() {
        var form = $(this);

        $.ajax({
            url: form.attr('action'),
            type: form.attr('method'),
            data: form.serialize(),
            dataType: 'json',
            success:function(response) {
            console.log(response);              
                if(response.success) {
                    //redirect main page
                    location.reload();
                }
                else {
                    $("#loginmsg").html(response.messages);

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

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

                        $(element).after(value);

                    });
                }
            } // /success
        });  // /ajax

        return false;
    });
});

1 个答案:

答案 0 :(得分:3)

因为location.reload()会重新加载您的login()页面,在这里您需要添加检查用户是否有logged_in的方法吗?

public function login()
{

    $logged_in = $this->session->userdata('logged_in');

    if($logged_in)
    {
        // example redirect to dashboard
        redirect('dashboard', 'refresh');
    }


    // rest of code
}

而只是echo json_encode($validator);更好地将您的内容标题设置为application/json

return $this->output->set_content_type('application/json')->set_output(json_encode($this->response));