te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Check whether a PHP session exists or expired using AJAX - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Check whether a PHP session exists or expired using AJAX - Stack Overflow

programmeradmin2浏览0评论
  1. How to set the php session time out, I'm trying like below, but I dont think it works

    ini_set("session.gc_maxlifetime", 600);

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

Regards

  1. How to set the php session time out, I'm trying like below, but I dont think it works

    ini_set("session.gc_maxlifetime", 600);

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

Regards

Share Improve this question edited Apr 8, 2011 at 19:10 user142162 asked Apr 8, 2011 at 18:59 user237865user237865 1,2504 gold badges19 silver badges41 bronze badges 4
  • 2 Dunno about the first but the second is not really possible: if you send AJAX requests on regular intervals you keep the session alive for good. – Shadow Wizard Commented Apr 8, 2011 at 19:02
  • @Shadow - while technically true if the session is cleared then a new session is recreated. He can use AJAX to check for a specific value – Cfreak Commented Apr 8, 2011 at 19:10
  • @Cfreak true, but if the AJAX is sent every minute for example, it will keep the session alive forever as far as I can tell. – Shadow Wizard Commented Apr 8, 2011 at 19:12
  • The session wouldn't get cleaned up if the AJAX script pings at an interval shorter than the expiring limit. however, the session will not just vanish after the timeout period. It's a probabilistic function, and can live anywhere from 0 hits to infinite hits AFTER the expiry time. – Marc B Commented Apr 8, 2011 at 19:36
Add a ment  | 

2 Answers 2

Reset to default 9

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

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 mon mistake)

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

<?php
session_start()

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

?>

On the Javascript side (using JQuery)

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

What Shadow Wizard mented 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.

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!

发布评论

评论列表(0)

  1. 暂无评论