在 Codeignitor php 7.4 中无法正确登录

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

当我单击“登录”按钮时,1)然后出现登录成功消息。 2)我还检查了会话是否已成功创建,因为我将响应行

$resp = array('status'=>'1','msg'=>$this->lang->line('ltr_logged_msg'),'url'=>$url);
放入条件
if ($this->session->has_userdata('email'))
内,它显示会话也已创建。 3)我还检查了整体代码并在浏览器中调试了代码。我找不到它返回登录页面的任何原因。这就是为什么我来这里寻求你的帮助。我附上了相关的View文件代码、Controller代码以及js文件代码,以便任何人都可以测试它。期待您的指导

查看Login.php

<form class="form" method="post" action="<?php echo base_url().'front_ajax/login'; ?>" data-redirect="yes">
    <div class="edu_field_holder">
        <input type="text" class="edu_form_field require" name="email" placeholder="<?php echo html_escape($this->common->languageTranslator('ltr_p_email'));?>" autocomplete="off" value="<?php echo(isset($_COOKIE['UML'])) ? base64_decode(urldecode(base64_decode($_COOKIE['UML']))) : ''; ?>">
    </div>
    <div class="edu_field_holder">
        <input type="password" name="password" class="require edu_form_field" placeholder="<?php echo html_escape($this->common->languageTranslator('ltr_password'));?>" value="<?php echo(isset($_COOKIE['SSD'])) ? base64_decode(urldecode(base64_decode($_COOKIE['SSD']))) : ''; ?>">
    </div>
    
    <div class="col-lg-6 col-md-6 col-sm-12 col-12 text-md-right">
        <button class="edu_btn edu_btn_black" id="auth_login" type="button" data-action="submitThisForm"><?php echo html_escape($this->common->languageTranslator('ltr_login'));?></button>
    </div>      
</form>

在控制器Front_ajax.php中

function login(){
    if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')){
        if(!empty($this->input->post('email',false)) && !empty($this->input->post('password',false))){          
            $email = trim($this->input->post('email',TRUE));
            $pass = md5(trim($this->input->post('password',TRUE)));     
            if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
                $stud_cond = array('enrollment_id'=>$email,'password'=>$pass);
            }else{
                $stud_cond = array('email'=>$email,'password'=>$pass);
            }
            $userDetails = $this->db_model->select_data('id,name,role,status,parent_id,teach_image,email,teach_batch,teach_subject,super_admin','users use index (id)',array('email'=>$email,'password'=>$pass),1);
            $studentDetails = $this->db_model->select_data('id,name,contact_no,batch_id,admin_id,enrollment_id,image,email,status,login_status','students use index (id)',$stud_cond,1);          
            $this->session->sess_destroy();
            if(!empty($userDetails)){
                  if($userDetails[0]['status']=='1'){
                    $brewers_strings = $this->random_strings(10);
                    $sess_arr = array(
                                'uid'=> $userDetails[0]['id'],
                                'name'=> $userDetails[0]['name'],
                                'role'=> $userDetails[0]['role'],
                                'status'=> $userDetails[0]['status'],
                                'admin_id' => $userDetails[0]['parent_id'],
                                'profile_img' => $userDetails[0]['teach_image'],
                                'email' => $userDetails[0]['email'],
                                'mobile' => $userDetails[0]['contact_no'],
                                'brewers_check' => $brewers_strings,
                                'super_admin' => $userDetails[0]['super_admin'],
                               
                            );
                        
                    $url = '';
                        $url = base_url().'admin/dashboard';
                                            
                    $this->session->set_userdata($sess_arr);
                        
                    $resp = array('status'=>'1','msg'=>$this->lang->line('ltr_logged_msg'),'url'=>$url);//
                    
                    $this->db_model->update_data_limit('users use index (id)',$this->security->xss_clean(array('token'=>1,'brewers_check'=>$brewers_strings)),array('id'=>$userDetails[0]['id']),1);
                  }else{
                    $resp = array('status' => '0','msg' =>$this->lang->line('ltr_contact_to_admin_msg'));//
                }
            }
            else{
                $resp = array('status' => '0','msg' =>$this->lang->line('ltr_wrong_credentials_msg'));
            }
        }
        else{
            $resp = array('status' => '0','msg' =>$this->lang->line('ltr_wrong_credentials_msg'));
        }
        echo json_encode($resp,JSON_UNESCAPED_SLASHES);
    }
    else{
        echo $this->lang->line('ltr_not_allowed_msg');
    } 
}

在login.js中

$(document).ready(function(){
$('[data-action="submitThisForm"]').on('click' , function(){
    var targetForm = $(this).closest('form');
    if(!myCustom.checkFormFields(targetForm)){
        myCustom.callFormAjax(targetForm).done(function(res){
            var resp = $.parseJSON(res);
            if(resp.status == 1){
                if(typeof targetForm.attr('data-reset') != 'undefined' && targetForm.attr('data-reset') == 1){ //check reset form data
                    targetForm[0].reset();
                }
                if(typeof targetForm.attr('data-redirect') != 'undefined'){ //check reset form data
                    if(resp.msg != '')
                        toastr.success(resp.msg)
                    setTimeout(function(){
                        location.href = resp.url;   
                    },1500)
                }else if(resp.msg){
                    toastr.success(resp.msg);
                }
            }else if(resp.status == 2){
                $.magnificPopup.open({
                    items: {
                        src: '#studentLogin',
                    },
                    type: 'inline'
                });
                $('#studentLogin .changeStudentLogin').attr('data-id',resp.student_id);
            }
            else if(resp.status == 0){
                toastr.error((resp.msg)?resp.msg:resp.error);
            }
        });
    }
});
javascript php codeigniter view controller
1个回答
0
投票

登录.php

       <form class="form" method="post" action="<?php echo base_url('front_ajax/login'); ?>" data-redirect="yes">
        <div class="edu_field_holder">
            <input type="text" class="edu_form_field require" name="email" placeholder="<?php echo html_escape($this->common->languageTranslator('ltr_p_email'));?>" autocomplete="off" value="<?php echo (isset($_COOKIE['UML'])) ? base64_decode(urldecode(base64_decode($_COOKIE['UML']))) : ''; ?>">
        </div>
        <div class="edu_field_holder">
            <input type="password" name="password" class="require edu_form_field" placeholder="<?php echo html_escape($this->common->languageTranslator('ltr_password'));?>" value="<?php echo (isset($_COOKIE['SSD'])) ? base64_decode(urldecode(base64_decode($_COOKIE['SSD']))) : ''; ?>">
        </div>
        
        <div class="col-lg-6 col-md-6 col-sm-12 col-12 text-md-right">
            <button class="edu_btn edu_btn_black" id="auth_login" type="button" data-action="submitThisForm"><?php echo html_escape($this->common->languageTranslator('ltr_login'));?></button>
        </div>      
    </form>
    
    Controller Method:
    
    public function login(){
        if($this->input->is_ajax_request()){
            $email = trim($this->input->post('email', TRUE));
            $password = trim($this->input->post('password', TRUE));
    
            if(!empty($email) && !empty($password)){
                // Use password_hash in the registration process and password_verify here
                if(filter_var($email, FILTER_VALIDATE_EMAIL)){
                    $user = $this->db_model->select_data('id, name, role, status, parent_id, teach_image, email, password, contact_no, super_admin', 'users', array('email' => $email), 1);
                } else {
                    $user = $this->db_model->select_data('id, name, contact_no, batch_id, admin_id, enrollment_id, image, email, password, status, login_status', 'students', array('enrollment_id' => $email), 1);
                }
    
                if(!empty($user) && password_verify($password, $user[0]['password'])){
                    if($user[0]['status'] == '1'){
                        $session_data = array(
                            'uid' => $user[0]['id'],
                            'name' => $user[0]['name'],
                            'role' => $user[0]['role'],
                            'status' => $user[0]['status'],
                            'admin_id' => isset($user[0]['parent_id']) ? $user[0]['parent_id'] : $user[0]['admin_id'],
                            'profile_img' => isset($user[0]['teach_image']) ? $user[0]['teach_image'] : $user[0]['image'],
                            'email' => $user[0]['email'],
                            'mobile' => $user[0]['contact_no'],
                            'brewers_check' => $this->random_strings(10),
                            'super_admin' => isset($user[0]['super_admin']) ? $user[0]['super_admin'] : null
                        );
    
                        $this->session->set_userdata($session_data);
    
                        // Update user session token
                        $this->db_model->update_data('users', array('token' => 1, 'brewers_check' => $session_data['brewers_check']), array('id' => $user[0]['id']));
    
                        $resp = array('status' => '1', 'msg' => $this->lang->line('ltr_logged_msg'), 'url' => base_url('admin/dashboard'));
                    } else {
                        $resp = array('status' => '0', 'msg' => $this->lang->line('ltr_contact_to_admin_msg'));
                    }
                } else {
                    $resp = array('status' => '0', 'msg' => $this->lang->line('ltr_wrong_credentials_msg'));
                }
            } else {
                $resp = array('status' => '0', 'msg' => $this->lang->line('ltr_wrong_credentials_msg'));
            }
    
            echo json_encode($resp, JSON_UNESCAPED_SLASHES);
        } else {
            echo $this->lang->line('ltr_not_allowed_msg');
        }
    }

Updated JavaScript (login.js):

$(document).ready(function(){
    $('[data-action="submitThisForm"]').on('click', function(){
        var targetForm = $(this).closest('form');
        if(!myCustom.checkFormFields(targetForm)){
            myCustom.callFormAjax(targetForm).done(function(res){
                var resp = $.parseJSON(res);
                if(resp.status == 1){
                    if(targetForm.attr('data-reset') == '1'){ // Reset form if required
                        targetForm[0].reset();
                    }
                    if(targetForm.attr('data-redirect') !== undefined){ // Redirect if required
                        if(resp.msg != '')
                            toastr.success(resp.msg);
                        setTimeout(function(){
                            location.href = resp.url;   
                        }, 1500);
                    } else if(resp.msg){
                        toastr.success(resp.msg);
                    }
                } else if(resp.status == 2){
                    $.magnificPopup.open({
                        items: {
                            src: '#studentLogin',
                        },
                        type: 'inline'
                    });
                    $('#studentLogin .changeStudentLogin').attr('data-id', resp.student_id);
                } else if(resp.status == 0){
                    toastr.error(resp.msg ? resp.msg : resp.error);
                }
            });
        }
    });
});
© www.soinside.com 2019 - 2024. All rights reserved.