自动使会话过期并检测会话是否已在Codeigniter中过期

时间:2022-10-19 20:46:47

I've seen plenty of questions here about my topic but it seems I still haven't found my answer. What I'm actually looking for is when the session expires the user will be automatically redirected to a page saying Your session has already expired. Please register to continue browsing..

我在这里看到很多关于我话题的问题,但似乎我还没有找到答案。我实际需要的是当会话到期时,用户将被自动重定向到一个页面,说明你的会话已经过期。请注册继续浏览..

Actually I have no idea on how I could determine if the user's session has already expired. Here's what I have so far.

实际上我不知道如何确定用户的会话是否已经过期。这是我到目前为止所拥有的。

Thanks.

谢谢。

function trial() {

    $this->db->select()->from('user')->where('user_type', 'tester');
    $tester = $this->db->get();

    foreach ($tester->result() as $row) {

        $data = array(
            'id' => $row->id,
            'first_name' => $row->first_name,
            'last_name' => $row->last_name,
            'display_name' => $row->display_name,
            'email' => $row->email,
            'avatar' => $row->avatar,
            'user_type' => $row->user_type,
            'logged_in' => TRUE
        );
        $this->session->set_userdata($data);
    }

    redirect(base_url(), 'refresh');
} 

5 个解决方案

#1


1  

you can store data in session object and user redirect any page , you can check session object already exist if not exist you can show alert, session is expired

您可以将数据存储在会话对象中并且用户可以重定向任何页面,您可以检查会话对象是否已经存在,如果不存在则可以显示警报,会话已过期

#2


6  

You can check user's session by making the ajax call and wrap it into the setInterval jquery's function like

您可以通过进行ajax调用来检查用户的会话,并将其包装到setInterval jquery的函数中,如

setInterval(function() {
  // Do something every 1 minute 
$.ajax({
type : 'POST',
url  : '<?php echo site_url("CONTROLLERNAME/check_session")?>'
success : function(data){
if(data){
   //your session is not expired
}else{
   //your session is already expired
 window.location.href="your url"; // or you can redirect from here also
} 
});
}, 60000);


 //This function checks your session 
 function check_session(){
    $id = $this->session->userdata('id');
   if($id ){
         echo 1;
   }else{
         echo 0;
   redirect('your register controller');//redirect from here or you can redirect in the ajax response
   }

  die();
 }

Hope it makes sense

希望它有意义

#3


1  

Although not 100% reliable, you could set a cookie on the users machine. I would recommend setting the expiry about an hour in advance.

虽然不是100%可靠,但您可以在用户计算机上设置cookie。我建议提前一小时设置到期时间。

With every page that you wanted to know if the session had expired, you could check to see if both the cookie and the session existed. If it does...continue. If only the cookie remains, then you know that the session has expired and you need to redirect them to the page that you referred to in your question. If neither are present, then there is a problem and the user needs to restart everything.

如果会话已过期,您想知道每个页面,您可以检查cookie和会话是否存在。如果确实......继续。如果只剩下cookie,那么您知道会话已过期,您需要将它们重定向到您在问题中引用的页面。如果两者都不存在,则存在问题,用户需要重新启动所有内容。

Of course, for every page that a session is instantiated, the session expiry will restart, so you will need to reset the cookie. It could be a bit tedious, but it is what I would recommend.

当然,对于会话实例化的每个页面,会话到期都将重新启动,因此您需要重置cookie。这可能有点乏味,但这是我推荐的。

#4


0  

When you are starting the session, store the current time in a session variable. You can use a session_expire_checker function which will check current time and stored time to determine whether the session has expired. You should call the function from the constructor so that every function call will use this function automatically. In that function if session is expired, you redirect to another controller.

启动会话时,将当前时间存储在会话变量中。您可以使用session_expire_checker函数来检查当前时间和存储时间,以确定会话是否已过期。您应该从构造函数中调用该函数,以便每个函数调用都将自动使用此函数。在该函数中,如果会话过期,则重定向到另一个控制器。

You have to use separate controller for the redirected URL, otherwise there would be infinite redirect.

您必须为重定向的URL使用单独的控制器,否则将存在无限重定向。

#5


0  

Codeigniter does not "fire an event" when the session expires. So either you write some code in each controller function to check if the user is still logged in. Or you use some small js in the footer (which I personally prefere):

Codeigniter在会话到期时不会“触发事件”。所以你要么在每个控制器函数中写一些代码来检查用户是否仍然登录。或者你在页脚中使用了一些小js(我个人喜欢):

<script>
    var mins = 15 * 60;
    var active = setTimeout("logout()",(mins*1000));
    function logout()
    {
        location='/account/timeout'; // <-- put your controller function here to destroy the session object and redirect the user to the login page.
    }
</script>

#1


1  

you can store data in session object and user redirect any page , you can check session object already exist if not exist you can show alert, session is expired

您可以将数据存储在会话对象中并且用户可以重定向任何页面,您可以检查会话对象是否已经存在,如果不存在则可以显示警报,会话已过期

#2


6  

You can check user's session by making the ajax call and wrap it into the setInterval jquery's function like

您可以通过进行ajax调用来检查用户的会话,并将其包装到setInterval jquery的函数中,如

setInterval(function() {
  // Do something every 1 minute 
$.ajax({
type : 'POST',
url  : '<?php echo site_url("CONTROLLERNAME/check_session")?>'
success : function(data){
if(data){
   //your session is not expired
}else{
   //your session is already expired
 window.location.href="your url"; // or you can redirect from here also
} 
});
}, 60000);


 //This function checks your session 
 function check_session(){
    $id = $this->session->userdata('id');
   if($id ){
         echo 1;
   }else{
         echo 0;
   redirect('your register controller');//redirect from here or you can redirect in the ajax response
   }

  die();
 }

Hope it makes sense

希望它有意义

#3


1  

Although not 100% reliable, you could set a cookie on the users machine. I would recommend setting the expiry about an hour in advance.

虽然不是100%可靠,但您可以在用户计算机上设置cookie。我建议提前一小时设置到期时间。

With every page that you wanted to know if the session had expired, you could check to see if both the cookie and the session existed. If it does...continue. If only the cookie remains, then you know that the session has expired and you need to redirect them to the page that you referred to in your question. If neither are present, then there is a problem and the user needs to restart everything.

如果会话已过期,您想知道每个页面,您可以检查cookie和会话是否存在。如果确实......继续。如果只剩下cookie,那么您知道会话已过期,您需要将它们重定向到您在问题中引用的页面。如果两者都不存在,则存在问题,用户需要重新启动所有内容。

Of course, for every page that a session is instantiated, the session expiry will restart, so you will need to reset the cookie. It could be a bit tedious, but it is what I would recommend.

当然,对于会话实例化的每个页面,会话到期都将重新启动,因此您需要重置cookie。这可能有点乏味,但这是我推荐的。

#4


0  

When you are starting the session, store the current time in a session variable. You can use a session_expire_checker function which will check current time and stored time to determine whether the session has expired. You should call the function from the constructor so that every function call will use this function automatically. In that function if session is expired, you redirect to another controller.

启动会话时,将当前时间存储在会话变量中。您可以使用session_expire_checker函数来检查当前时间和存储时间,以确定会话是否已过期。您应该从构造函数中调用该函数,以便每个函数调用都将自动使用此函数。在该函数中,如果会话过期,则重定向到另一个控制器。

You have to use separate controller for the redirected URL, otherwise there would be infinite redirect.

您必须为重定向的URL使用单独的控制器,否则将存在无限重定向。

#5


0  

Codeigniter does not "fire an event" when the session expires. So either you write some code in each controller function to check if the user is still logged in. Or you use some small js in the footer (which I personally prefere):

Codeigniter在会话到期时不会“触发事件”。所以你要么在每个控制器函数中写一些代码来检查用户是否仍然登录。或者你在页脚中使用了一些小js(我个人喜欢):

<script>
    var mins = 15 * 60;
    var active = setTimeout("logout()",(mins*1000));
    function logout()
    {
        location='/account/timeout'; // <-- put your controller function here to destroy the session object and redirect the user to the login page.
    }
</script>