Reading the servers whole SESSION data
Moderator: General Moderators
-
impulse()
- Forum Regular
- Posts: 748
- Joined: Wed Aug 09, 2006 8:36 am
- Location: Staffordshire, UK
- Contact:
Reading the servers whole SESSION data
I'm looking to create a function that will show all online users. The users are tracked through sessions but I've hit snatch because I'm not sure how I would access every session variable the server has allocated.
Or if there would be another way to do this I'd be grateful for your thoughts.
Regards,
Or if there would be another way to do this I'd be grateful for your thoughts.
Regards,
feyd | Please use
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I think the easiest way would be to create a database table to track when a user logs in/out of your system. This would allow you to display the stats you are looking for as well as some other semi-resourceful things such as tracking the most common/popular times for using your system.
However, I think that if you really wanted to search all the sessions, the only way I can think of to do this would be to find the directory that your session files are stored in and count the number of files there. If you properly destroy each session upon a user logging out, this should be a fairly accurate count, unless you initialize a session for users that aren't logged in. If you do this, it's a bit more complicated and possible inefficient. Something like...Code: Select all
function countLoggedIn()
{
$files = glob("path_to_files/*"); // glob all files in the session directory
foreach ($files as $key => $file)
{
$file = str_replace("path_to_files","",$file);
session_id($file);
session_start();
if (isset($_SESSION['logged_in']))
{
$count++;
}
session_destroy();
}
return(count);
}feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
-
impulse()
- Forum Regular
- Posts: 748
- Joined: Wed Aug 09, 2006 8:36 am
- Location: Staffordshire, UK
- Contact:
I've read through a few pages about sessions and I have come up with the following code which doesn't seem to work as expected. I was hoping after 5 seconds of not requesting the page that the value would reset.
Can you see where I'm going wrong with this?
Code: Select all
ini_set('session.gc_maxlifetime', 5);
session_start();
++$_SESSION['count'];
echo $_SESSION['count'];
-
impulse()
- Forum Regular
- Posts: 748
- Joined: Wed Aug 09, 2006 8:36 am
- Location: Staffordshire, UK
- Contact:
I think I agree. I just thought of a simple solution.
On each page insert into a database the username from the session, if set, and a timestamp. Then a page to show online uses calculated by fetching all rows from that database where the timestamp is within the last x seconds. And maybe a CRON to cleanup the database on a daily basis.
On each page insert into a database the username from the session, if set, and a timestamp. Then a page to show online uses calculated by fetching all rows from that database where the timestamp is within the last x seconds. And maybe a CRON to cleanup the database on a daily basis.