Forums / Developer / Fetching logged in users using PHP

Fetching logged in users using PHP

Author Message

Damien MARTIN

Thursday 14 January 2010 12:53:41 am

Hi there,

I have a problem fetching users and filtering them by "logged in" status :

$membres = eZContentObjectTreeNode::subTreeByNodeID(false, 253);
            
foreach($membres as $membre){
                
    $usr = eZUser::instance($membre->ContentObjectID);
    $nod = eZContentObject::fetch($membre->ContentObjectID);
                
    if($usr->isLoggedIn() == true)
        $liste[] = array("nom" => $nod->Name, "id" => $nod->ID);
                
}

But when I look to my $liste, everybody is in, although everybody is not logged in. (verified in the administration panel)

For information, I really need this code because I need to filter the users by another added attribute too. And as the web site will have a lot of users. I prefer to do this kind of operations directly in PHP and not with the template language (for speed).

Thanks,

Damien

Gaetano Giunta

Thursday 14 January 2010 1:14:22 am

If speed is your concern, I'd suggest going for a custom sql request; With many users the above code will be doing a huge amount of queries, wasting time and memory...

Principal Consultant International Business
Member of the Community Project Board

Bertrand Dunogier

Thursday 14 January 2010 1:21:30 am

I'm not sure why this would fail, but it might be more efficient to use eZUser::fetchLoggedInList(). But maybe you need to filter this list out by group, in which case custom code would indeed be required.

Bertrand Dunogier
eZ Systems Engineering, Lyon
http://twitter.com/bdunogier
http://gplus.to/BertrandDunogier

Damien MARTIN

Thursday 14 January 2010 1:46:42 am

@Gaetano

I would like to create custom SQL queries but I don't have the time for now to look at the database.
I looked some months ago for a diagramm with all the tables and relations of the eZ database but unsuccessfully.

@Bertrand

eZUser::fetchLoggedInList() works very well for what I want to do. I just now have to fetch using eZContentObject::fetch() and all the datas I need are there.
I suppose it would be faster with this method (no more need to fetch eZUser).
I don't know when I haven't tried this method before...

Thank you very much guys !

eZ debug

Timing: Jan 29 2025 13:44:49
Script start
Timing: Jan 29 2025 13:44:49
Module start 'content'
Timing: Jan 29 2025 13:44:49
Module end 'content'
Timing: Jan 29 2025 13:44:49
Script end

Main resources:

Total runtime0.0176 sec
Peak memory usage2,048.0000 KB
Database Queries4

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0070 587.9531180.8281
Module start 'content' 0.00700.0056 768.781398.1016
Module end 'content' 0.01260.0049 866.882874.7031
Script end 0.0175  941.5859 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.002212.7120120.0002
Check MTime0.00105.6246120.0001
Mysql Total
Database connection0.00084.317710.0008
Mysqli_queries0.003117.399440.0008
Looping result0.00000.182820.0000
Template Total0.004626.010.0046
Template load0.00105.539310.0010
Template processing0.003620.399310.0036
Override
Cache load0.00073.873410.0007
General
dbfile0.00105.6544100.0001
String conversion0.00000.050130.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs