Redirecting to the original page after logging in (Codeigniter)

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

Problem: When a user who is not registered in types in the URL, for example http://www.domain.com/listings/1234, is shown this page, but with a darkened page that prevents the user from interacting with the page (jQuery, no problem here) and a pop-up modal field with a link to the login page (Tank Auth) http://www.domain.com/login.

After entering the login page, the user will be redirected back to the normal page after entering the system, that is:, http://www.domain.combut a variable is passed to this page 1234.

In short : I want the user to not log in and enter the website in http://www.domain.com/listings/1234so as NOT to redirect to http://www.domain.com/login, but instead stayed on http://www.domain.com/listings/1234and there was a modal box shown with a link to the login page http://www.domain.com/login, where if he logs in, he will be redirected back to http://www.domain.com/instead of the regular page that it will receive after logging in, and pass the variable 1234.

How can this be done with Codeigniter?

This is what I have now:

Controller list

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);

}
+2
source share
1 answer

There are several ways to resolve your issue. We hope that this solution will give some ideas on how to do this.

jquery ui jquery .ajax .

</body>, .

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

ajax

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);
}

, codeigniter, ajax.

, , .

+2

All Articles