使用AJAX检查PHP会话是否存在或过期

时间:2022-02-04 21:02:41
  1. How to set the php session time out, I'm trying like below, but I dont think it works

    如何设置php会话时间,我尝试如下,但我不认为它有效

    ini_set("session.gc_maxlifetime", 600);

    报错(“会话。gc_maxlifetime ",600);

  2. How to find out whether a php session exists or expired using ajax (javascript)?

    如何使用ajax (javascript)查找php会话是否存在或过期?

Regards

问候

2 个解决方案

#1


8  

For #1 use session_set_cookie_params(). To expire after 600 seconds

# 1使用session_set_cookie_params()。在600秒后过期

session_set_cookie_params(600)

(note unlike the regular setcookie function the session_set_cookie_params uses seconds you want it to live, it should not be time() + 600 which is a common mistake)

(注意不像常规的setcookie函数session_set_cookie_params使用你想要的时间,它不应该是时间()+ 600,这是一个常见的错误)

For number 2 just make a small script called through AJAX:

对于第二个问题,只需编写一个通过AJAX调用的小脚本:

<?php
session_start()

if( empty($_SESSION['active']) ) {
     print "Expired"
}
else {
     print "Active"
}

?>

On the Javascript side (using JQuery)

在Javascript方面(使用JQuery)

$.get('path/to/session_check.php', function(data) {
     if( data == "Expired" ) {
         alert("Session expired");
     } else if (data == "Active" ) {
         alert("Session active");
     }
 });

#2


2  

What Shadow Wizard commented about keeping the session alive every time you do the check is true.

当你每次做检查时,影子向导都在评论保持会话的存活。

But the solution is quite simple. The trick is to perform the AJAX request at an interval larger than the session life time. So if you establish a session timeout of 15 minutes you can check via AJAX every 16 or more.

但解决方法很简单。诀窍是在比会话生命周期更长的时间间隔内执行AJAX请求。因此,如果您建立了15分钟的会话超时,您可以每16分钟或更长时间通过AJAX检查一次。

In order for the above to work, the session timeout is something that you should implement manually. You can read this usefull answer on how to set the session duration.

为了使以上内容有效,您应该手动实现会话超时。您可以阅读关于如何设置会话持续时间的完整答案。

Hope this helps you or anyone who is looking for the same!

希望这能帮助你或任何正在寻找相同的人!

#1


8  

For #1 use session_set_cookie_params(). To expire after 600 seconds

# 1使用session_set_cookie_params()。在600秒后过期

session_set_cookie_params(600)

(note unlike the regular setcookie function the session_set_cookie_params uses seconds you want it to live, it should not be time() + 600 which is a common mistake)

(注意不像常规的setcookie函数session_set_cookie_params使用你想要的时间,它不应该是时间()+ 600,这是一个常见的错误)

For number 2 just make a small script called through AJAX:

对于第二个问题,只需编写一个通过AJAX调用的小脚本:

<?php
session_start()

if( empty($_SESSION['active']) ) {
     print "Expired"
}
else {
     print "Active"
}

?>

On the Javascript side (using JQuery)

在Javascript方面(使用JQuery)

$.get('path/to/session_check.php', function(data) {
     if( data == "Expired" ) {
         alert("Session expired");
     } else if (data == "Active" ) {
         alert("Session active");
     }
 });

#2


2  

What Shadow Wizard commented about keeping the session alive every time you do the check is true.

当你每次做检查时,影子向导都在评论保持会话的存活。

But the solution is quite simple. The trick is to perform the AJAX request at an interval larger than the session life time. So if you establish a session timeout of 15 minutes you can check via AJAX every 16 or more.

但解决方法很简单。诀窍是在比会话生命周期更长的时间间隔内执行AJAX请求。因此,如果您建立了15分钟的会话超时,您可以每16分钟或更长时间通过AJAX检查一次。

In order for the above to work, the session timeout is something that you should implement manually. You can read this usefull answer on how to set the session duration.

为了使以上内容有效,您应该手动实现会话超时。您可以阅读关于如何设置会话持续时间的完整答案。

Hope this helps you or anyone who is looking for the same!

希望这能帮助你或任何正在寻找相同的人!