Redirect to original page after logging in (Codeigniter)

限于喜欢 提交于 2019-12-06 08:43:53
12Bo

There are several ways that your question can be accomplished. Hopefully this solution would give some ideas on how to accomplish this.

The solution below uses jquery ui dialog to display the modal and jquery .ajax to handle the login form submission.

Conditionally put this code at the bottom of you page before the </body> tag if the user is not logged in.

<?php if($this->auth->tank_auth->is_logged_in()){ ?>
    <div id="dialog-wrap" class="modal" >
    <?php echo $this->load->view('login_form'); ?>
    </div>
    <script>
        $('#dialog-wrap').modal({
            modal: true,
            autoOpen: true,
        });

        $('#login-form').submit(function(){
            //use .ajax to submit login form and check credentials.
            $.ajax({
                type: 'POST',
                url: <?php echo base_url(); ?>"login.php",
                data: "username="+$('username').val()+'&password='+$('password').val(),
                dataType: 'json',
                success: function(returnData){.
                    if(returnData.status){
                        //close modal box
                    }else {
                        //display message that login failed.
                    }
                }
            });

        });
    </script>
?>

Your ajax login controller

function login(){

  //Use tank auth to validate login information.

  if(authentication is successful) {
    $return_data = array(
        'status'    => true,
        'msg'           => 'Login Successful'
    );
  }else {
        $return_data = array(
        'status'    => false,
        'msg'           => 'Login failed'
    );
  } 

  //Use echo instead of return when you are sending data back to an jquery ajax function.
  echo json_encode($data);
}

If you do it this way then you won't have to rely on codeigniter redirects everything will be handled through ajax.

I hope this gives you an idea of how to handle this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!