登录后重定向到原始页面(Codeigniter) [英] Redirect to original page after logging in (Codeigniter)

查看:128
本文介绍了登录后重定向到原始页面(Codeigniter)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

整个网站需要用户在查看网页之前登录。如果用户未登录,则会被重定向到登录/注册页面。



问题:在类似于 http://www.domain.com/listings/1234 的网址中输入网址,他会显示该网页,但是网页变暗,导致用户无法互动与页面(jQuery的东西,这里不是一个问题),以及一个弹出式模式框,指向(Tank Auth)登录页面的链接 http://www.domain.com/login



从登录页面登录后,用户将被重定向回登录后获得的普通页面,即: http://www.domain.com ,但该页面通过变量 1234



简而言之:我希望尚未登录并已进入网站的用户 http://www.domain.com/listings/1234 code>到 重定向到 http://www.domain.com/login 而是保留在 http://www.domain.com/listings/1234 ,并显示一个包含登录页面链接的模式框 http ://www.domain.com/login 如果他登录,他将被重定向回 http://www.domain.com/ ,而不是他在登录后获得的通常页面,并且传递变量 1234





> Listings controller

 函数索引(listing_id){
$ this-> load->模块('auth');
if(!$ this-> auth-> tank_auth-> is_logged_in()){
redirect('login');
}

//这里调用数据库等

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

}


解决方案

几种方式,你的问题可以完成。希望这个解决方案将给出一些如何完成这一点的想法。



下面的解决方案使用jquery ui对话框显示模式和jquery .ajax 来处理登录表单提交。



如果用户未登录,有条件地将此代码放在页面底部的< / body> p>

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

$('#login-form')。submit(function(){
//使用.ajax提交登录表单并检查凭证
$ .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){
//关闭模式框
} else {
//显示登录失败的消息
}
}
} );

});
< / script>
?>

您的ajax登录控制器

  function login(){

//使用tank auth验证登录信息。

if(认证成功){
$ return_data = array(
'status'=> true,
'msg'=&
);
} else {
$ return_data = array(
'status'=> false,
'msg'=>'登录失败'
);
}

//当你将数据发送回jquery ajax函数时,使用echo而不是return。
echo json_encode($ data);
}

如果你这样做,那么你不必依赖codeigniter重定向一切都将通过ajax处理。



我希望这给你一个如何处理这个的想法。


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

}

解决方案

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.

这篇关于登录后重定向到原始页面(Codeigniter)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆