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 关于#tensorflow#的问题:有没有什么方法可以让机器自己学会像素风格的图片
    • ¥15 Oracle触发器字段变化时插入指定值
    • ¥15 docker无法进入容器内部
    • ¥15 qt https 依赖openssl 静态库
    • ¥15 python flask 报错
    • ¥15 改个密码引发的项目启动问题
    • ¥100 CentOS7单线多拨
    • ¥15 debian安装过程中老是出现无法将g21dr复制到g21dr怎么解决呀?
    • ¥15 如何用python实现跨工作簿的指定区域批量复制粘贴
    • ¥15 基于CH573f的雷迪安CR1400m通讯代码