On Fri, 2 Jul 2004 01:13:14 -0400, Matt Palermo <[EMAIL PROTECTED]> wrote:
> 
> Is it possible to make a table of all logged in users session ids and then
> check to see if the session still exists every couple minutes?  So every
> minute or two, it would go through the table and for each record it would
> get the session id, then check that session id to see if it's still in
> existance.  Is this possible to do?

This is simple to accomplish if you use database managed PHP sessions.
 You can override the default PHP session handling functions with
session_set_save_handler() then you will know what users are logged in
by what sessions are still active.  Here's how I do it:

My sessions.php that I require in all other scripts on the site:

<?php

if(ini_get('session.use_trans_sid')==0){
    ini_set("session.use_trans_sid", 1);
}

$sessionDbHandler = '';

function sessionOpen($save_path, $session_name){
global $sessionDbHandler;
        if(! $sessionDbHandler = mysql_pconnect(DB_HOST, DB_USER, DB_PASS)){
                die("Can't get sessionDbHandler: ".mysql_error());
        }
        return true;
}

function sessionClose(){
        return true;
}

function sessionRead($key){
        $sql = "
                select
                        sessionData
                from
                        ".TB_USER_SESSIONS."
                where
                        sessionID = '$key'
                and
                        sessionExpire > UNIX_TIMESTAMP()
        ";
        $query = mysql_query($sql);
        if(mysql_num_rows($query)){
                return mysql_result($query, 0, "sessionData");
        }
        return false;
}

function sessionWrite($key, $val){
        $value = addslashes($val);
        $sql = "
                replace into ".TB_USER_SESSIONS." (
                                sessionID,
                                sessionData,
                                sessionExpire
                ) values (
                        '$key',
                        '$value',
                        UNIX_TIMESTAMP() + ".SITE_ONLINE_EXPIRE."
                )
        ";
        return mysql_query($sql);
}

function sessionDestroy($key){
        $sql = "
                delete from
                        ".TB_USER_SESSIONS."
                where
                        sessionID = '$key'
        ";
        return mysql_query($sql);
}

function sessionGarbageCollection(){
        $sql = "
                delete from
                        ".TB_USER_SESSIONS."
                where
                        sessionExpire < UNIX_TIMESTAMP()
        ";
        $query = mysql_query($sql);
        return sqlAffectedRows();
}

session_set_save_handler("sessionOpen", "sessionClose",
"sessionRead",  "sessionWrite", "sessionDestroy",
"sessionGarbageCollection");
session_start();

?>

Some config.php variables:
define('DB_HOST', 'localhost');
define('DB_DB', 'db_name');
define('DB_USER', 'db_user');
define('DB_PASS', 'db_pass');
define('TB_USER_SESSIONS', 'userSessions');
define('SITE_ONLINE_EXPIRE', 900);

The table structure for userSessions looks like this:

CREATE TABLE `userSessions` (
  `sessionID` varchar(32) NOT NULL default '',
  `sessionData` text NOT NULL,
  `sessionExpire` int(10) unsigned NOT NULL default '0',
  PRIMARY KEY  (`sessionID`)
) TYPE=MyISAM;

When a user logs in you are probably already setting some kind of
userID or userName, so make that a session variable if it is not
already.  Then it's just a matter of listing the userNames or userIDs
out of the userSessions table.

You might want to force session garbage collection at the top of each
script by calling sessionGarbageCollection().  That will make the
username listings more realtime.


-- 
Greg Donald
http://destiney.com/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to