如果会话不存在,Codeigniter Ajax函数不会重定向到登录页面 [英] Codeigniter ajax function not redirecting to login page if session does not exists

查看:74
本文介绍了如果会话不存在,Codeigniter Ajax函数不会重定向到登录页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为具有用户注册功能的Web应用程序使用codeigniter。而且我在大多数地方都使用ajax来加载页面。问题是,如果通过ajax调用了某些函数来加载页面,并且如果会话不存在,则会话不会重定向到登录页面。但是,当我们通过浏览器访问相同的URL时,它是有效的(它正在重定向到登录页面)。我应该怎么做才能解决这个问题?

I'm using codeigniter for my web application which is having user registration feature. And also i am using ajax in most of the places for loading the page. The problem is, if some function is called through ajax to load a page and if session does not exists, it is not redirecting to the login page. But it is working when we visits the same URL through browser(It is redirecting to the login page). What should i do to resolve this problem?

这是检查我们正在使用的会话的代码。

This is the code for checking session we are using.

    $userid=$this->phpsession->get('userid');
    if(!($userid))
    { 
        redirect('home','refresh');
    }


推荐答案

在您的控制器中检测是否是一个ajax请求:

In your controller detect if it is an ajax request:

if($this->input->is_ajax_request())
{
    $userid=$this->phpsession->get('userid');
    if(!($this->usermodel->is_user($userid)))
    { 
        echo "1";
    }
}

在ajax中,检测响应并重定向到登录页面:

In ajax detect the response and redirect to the login page:

$.post('index.php/login', function(data){

if(data == "1")
{
document.location.href = <?php echo site_url("home"); ?>;
}

});

这篇关于如果会话不存在,Codeigniter Ajax函数不会重定向到登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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