Page 1 of 1

sessions

Posted: Mon Oct 13, 2003 10:44 am
by gurjit
hi all,

how do i get a list of all the active sessions on the server.

i have a session called $my_name. i want to know how many current active sessions there are with the name $my_name?

Posted: Mon Oct 13, 2003 12:16 pm
by Gen-ik
Correct me If I'm wrong here but I don't think this is possible in most cases. If your website is hosted on a server you don't have control of then the session info is normally stored 'out of reach' of any websites hosted on that server. If the session info is stored client-side then you can't access that with PHP either.

There may be a way around it but as far as I know it's not possible which is why people use a database to store and display users who are currently 'active' on websites.

Posted: Mon Oct 13, 2003 1:17 pm
by liljester
if its your server, and you know where your server session files are stored, you could try to read each one of the session files. ive never tried it, not even sure if itll work. but an interesting idea mabe?

Posted: Mon Oct 13, 2003 3:11 pm
by logand
This is a copy of http://logand.users.phpclasses.org/brow ... /1354.html
code... please check the site to get updates.
<?

//########################################################################################
// -------------- Summary
// Lists all she sessions available on a server. Need read access to temps sessions dir.
//
// -------------- Author
// Logan Dugenoux - 2003
// logan.dugenoux@netcourrier.com
// http://www.peous.com/logan/
//
// -------------- License
// GPL
//
// -------------- Methods :
// - getSessionsCount()
// - getSessions()
//
// ------------- Example :
// require ("sessionLister.php");
// $sl = new sessionLister();
// echo $sl->getSessionsCount()." sessions available<br>";
// foreach( $sl->getSessions() as $sessName => $sessData )
// {
// echo "Session ".$sessName." : Rawdata = ".$sessData."<br>";
// }
//
// Have fun !!!
//
//########################################################################################

class sessionLister
{
var $diffSess;

function sessionLister()
{
}

function getSessionsCount()
{
if (!$this->diffSess)
readSessions()
return sizeof($this->diffSess);
}

function getSessions()
{
if (!$this->diffSess)
readSessions()
return &$this->diffSess;
}

//------------------ PRIVATE ------------------
function readSessions()
{
$sessPath = get_cfg_var("session.save_path")."\\";
$diffSess = array();

$dh = @opendir($sessPath);
while(($file = @readdir($dh)) !==false )
{
if($file != "." && $file != "..")
{
$fullpath = $sessPath.$file;
if(!@is_dir($fullpath))
{
// "sess_7480686aac30b0a15f5bcb78df2a3918"
$fA = explode("_", $file);
// array("sess", "7480686aac30b0a15f5bcb78df2a3918")
$sessValues = file_get_contents ( $fullpath ); // get raw session data
// this raw data looks like serialize() result, but is is not extactly this.
$this->diffSess[$fA[1]] = $sessValues;
}
}
}
@closedir($dh);
}
}
?>

Posted: Mon Oct 13, 2003 4:19 pm
by Gen-ik
Yeah like I thought you need to have access to the folder where the sessions are stored... which in most cases is not possible.

Posted: Mon Oct 13, 2003 5:58 pm
by JAM
You can also use a database for this. That really sounds like a more simplified solution of you are lacking read-access to the /tmp.

Set the session, store it in the database. You wont know when the user closes his browser ofcourse, so you need to find a solution that fits you to automaticly remove the 'old' sessions from the database.

Search for a "There are X people online right now" onliners-script, and take the ideas from those.

Don't know if its a big deal, but if I'd have to choose, I'd choose a host that hasn't opened up the /tmp dir... Sounds to much like a possible security issue.