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 !