Redirect to original page after logging in (Codeig

2019-05-18 21:27发布

The entire website requires a user to login before viewing the page. If the user is not logged in, he gets redirected to the login/registration page.

Problem: When a user that is not logged in types in a url like http://www.domain.com/listings/1234 , he will be shown that page, but with a darkened page that prevents the user from interacting with the page (jQuery stuff, not a problem here), and a popup modal box with a link to the (Tank Auth) login page http://www.domain.com/login.

After logging in from the login page, the user will be redirected back to the usual page one gets after logging in, ie: http://www.domain.com but that page is passed the variable 1234.

In short: I want the user that has not logged in and has entered the website at http://www.domain.com/listings/1234 to NOT be redirected to http://www.domain.com/login yet, but instead remain on http://www.domain.com/listings/1234 and be shown a modal box with a link to the login page http://www.domain.com/login where if he logins in, he will be redirected back to http://www.domain.com/ instead of the usual page he gets after login and be passed the variable 1234.

How can this be done using Codeigniter?

This is what I have now:

Listings controller

function index(listing_id) {
    $this->load->module('auth');
    if(!$this->auth->tank_auth->is_logged_in()) {
         redirect('login');
    } 

    // Some calls to database etc here...

    $this->load->view('listings', $data);

}

1条回答
唯我独甜
2楼-- · 2019-05-18 22:16

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.

查看更多
登录 后发表回答