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 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘