doutizong8099
doutizong8099
2017-11-03 16:34
浏览 61

会话闪存数据无法在ajax中运行

there is a change password feature in my admin now before changing password i am validating by asking user to enter the username password again now if the user password is incorrect what i am doing is setting flash data and logging the user out. so when he comes to main login panel he should see the error now here is my code

the controller method

public function checkpassword(){
        $this->check_unique_id_from_db_for_secuity();
        $unique_id=base64_decode($this->uri->segment(3));
        $data = $this->input->post();
        $data['unique_id']=$unique_id;
        $status=$this->User_model->validate_user_loged_in_for_change_password($data);
        if($status){
            echo "true";
            exit();
        }
        else
        {
            $this->session->set_flashdata('change_password_credentials_error',"Your Username/Password was incorect account was logged out for secutiry reasons!");
            echo "false";
            exit();
        }
    }

the ajax request

$.ajax({
            type: 'post',
            url: '<?php echo base_url()."User/checkpassword/".$this->uri->segment(3).""; ?>',
            data:{ "login_username" : $("#login_username").val(), "login_password" : $("#login_password").val() },
            success: function (data) {
                if(data=="true"){
                    window.location.href="<?php echo base_url()."User/changepassword_view/".$this->uri->segment(3)."";?>";
                }
                else
                {
                    window.location.href="<?php echo base_url()."User/logout";?>";
                }
            }

        });

the php script on main login page

<?php
    if($this->session->flashdata('change_password_credentials_error')){?>
        <script>toastr["success"]("<?php echo $this->session->flashdata('change_password_credentials_error')?>");</script>
    <?php } ?>

now the flash data is working just fine i am using it in many other pages but when i am using it with ajax somehow it is not being set! any help?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doulan8846
    doulan8846 2017-11-03 16:47
    已采纳

    What i get about flashdata from codeigniter doc:

    CodeIgniter supports “flashdata”, or session data that will only be available for the next request, and is then automatically cleared.

    From above description, your change_password_credentials_error session will only available at "User/Logout" controller and cleared after controller processed. So, now after then redirected to Login Page the flashdata session will not available.

    For this case, just clear login session after $status variable (else) checking, and then directly move / redirect user to main login page.

    点赞 评论

相关推荐