sesscount does count sessions that are still active by checking if the UPDATED timestamp for an sid (session id) is bigger than the submitted timestamp. So, if we want to see how many sessions are still ongoing for the last x mintues we can do so easiy, splitting it even by guests and users..

Now, WHAT ABOUT if i only want to see new sessions since last time I have checked? I cannot do so just be seeing if the sid's timestamp is bigger than my "pointer in the past.."

So, i would like to see this sess_count function expanded with this feature "$onlynew":

current version:

function sess_count($timestamp = 0, $anonymous = true) {
  $query = $anonymous ? ' AND uid = 0' : ' AND uid > 0';
  return db_result(db_query('SELECT COUNT(sid) AS count FROM {sessions} WHERE timestamp >= %d'. $query, $timestamp));
}

my version:

function sess_count($timestamp = 0, $anonymous = true, $onlynew = false) {
  $query = $anonymous ? ' AND uid = 0' : ' AND uid > 0';
  $query2= $onlynew ? '' : ' AND sid NOT IN (select sid from {sessions} where $timestamp < %d)';
  return db_result(db_query('SELECT COUNT(sid) AS count FROM {sessions} WHERE timestamp >= %d'. $query, $timestamp));

For example, now one can easily, also the beginner, count new guests for a the last our without having to count the active guests AGAIN!

Comments

OSLinux’s picture

Sorry, Typo: we need to also add ". $query2" :)


function sess_count($timestamp = 0, $anonymous = true, $onlynew = false) {
  $query = $anonymous ? ' AND uid = 0' : ' AND uid > 0';
  $query2= $onlynew ? '' : ' AND sid NOT IN (select sid from {sessions} where $timestamp < %d)';
  return db_result(db_query('SELECT COUNT(sid) AS count FROM {sessions} WHERE timestamp >= %d'. $query .$query2, $timestamp));
}
OSLinux’s picture

Function write from session.inc

function sess_write($key, $value) {
  global $user;

  // If the client doesn't have a session, and one isn't being created ($value), do nothing.
  if (empty($_COOKIE[session_name()]) && empty($value)) {
    return TRUE;
  }

  $result = db_query("SELECT sid FROM {sessions} WHERE sid = '%s'", $key);

  if (!db_num_rows($result)) {
    // Only save session data when when the browser sends a cookie. This keeps
    // crawlers out of session table. This improves speed up queries, reduces
    // memory, and gives more useful statistics. We can't eliminate anonymous
    // session table rows without breaking throttle module and "Who's Online"
    // block.
    if ($user->uid || $value || count($_COOKIE)) {
      db_query("INSERT INTO {sessions} (sid, uid, cache, hostname, session, timestamp, <strong>sessionstart</strong>) VALUES ('%s', %d, %d, '%s', '%s', %d, %d)", $key, $user->uid, $user->cache, $_SERVER["REMOTE_ADDR"], $value, time(), <strong>time()</strong>);
    }
  }
  else {
    db_query("UPDATE {sessions} SET uid = %d, cache = %d, hostname = '%s', session = '%s', timestamp = %d WHERE sid = '%s'", $user->uid, $user->cache, $_SERVER["REMOTE_ADDR"], $value, time(), $key);

    // TODO: this can be an expensive query. Perhaps only execute it every x minutes. Requires investigation into cache expiration.
    if ($user->uid) {
      db_query("UPDATE {users} SET access = %d WHERE uid = %d", time(), $user->uid);
    }
  }

  return TRUE;

:: Need to add a field manually called sessionstart that once gets filled with time() and will never be updated!

Thus we can make the codeabove work by changing the subquery into: 'where sid NOT IN (select sid from {sessions} where sessionsstart< %d', timestamp"

OSLinux’s picture

Status: Needs review » Reviewed & tested by the community

Ok, I see it now too, to make it much easier on the server load :)


($timestamp = 0, $anonymous = true, $onlynew = true) {

  $query = $anonymous ? ' AND uid = 0' : ' AND uid > 0';
  $query2= $onlynew ? ' AND sessionstart >= %d ' : ' AND sessionstart <= %d ';

  return db_result(db_query('SELECT COUNT(sid) AS count FROM {sessions} WHERE timestamp >= %d'. $query .$query2, $timestamp, $timestamp));

sun’s picture

Status: Reviewed & tested by the community » Needs work

Please attach a patch, see http://drupal.org/patch/create

sun’s picture

Component: other » user system
Category: feature » bug
chx’s picture

Category: bug » feature
pancho’s picture

Version: 6.x-dev » 7.x-dev

No more API changes in D6. Bumping to D7.

Jaza’s picture

Version: 7.x-dev » 8.x-dev

Moving.

pfrenssen’s picture

Status: Needs work » Fixed

This function has been removed from D7.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.