sessions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

sessions

Post 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?
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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.
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post 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?
logand
Forum Newbie
Posts: 2
Joined: Mon Oct 13, 2003 3:11 pm

Post 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);
}
}
?>
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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.
Post Reply