SMF check if the user is logged on

Started by Laloeka, May 05, 2012, 11:05:51 AM

Previous topic - Next topic

Laloeka

Hello community,

I was searching how to do this, but I couldn't really find it anywhere. My goal is to make a chat-window in the theme template, which only shows when the user is logged on. That by itself isn't really hard to do.

My question is: How can I check if an user has logged on to the forum from another PHP file (for example: chat.php), where the chat messages are sent to, and are received by the chat-window in the forum page. I know it's possible, but I'm not sure how.

Can anyone help me with this? :)

Thanks!

NanoSector

Hi Laloeka, welcome to SMF!

Have you already included SSI.php in that PHP file?

If so, do this for every user:
loadMemberData(userid);

And don't forget to global $user_profile.

Then, you can use this:
$is_online[id] = $user_profile[id]['is_online'] === 0 ? false : true;
Replacing id with the user ID.

Then you can use $is_online[id] to check if an user is online.
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

Laloeka


NanoSector

No problem :)
If your topic is solved, please hit that green button below :)
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

Robert.

Ehm shouldn't this be in coding discussion? :P

NanoSector

Quote from: 医生唱片骑师 on May 05, 2012, 12:10:19 PM
Ehm shouldn't this be in coding discussion? :P
I already reported it to the team, I think it gets moved in a while.
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

Arantor

QuoteIf so, do this for every user:

Running 2 or maybe 3 queries for every user, that's nice and efficient, isn't it? Pass an array of ids to loadMemberData rather than an individual member id at a time, so you only do one query for all of them.

Or you could query the {db_prefix}log_online table and get the id_members of all online members directly from there...

NanoSector

Quote from: Arantor on May 05, 2012, 12:15:30 PM
QuoteIf so, do this for every user:

Pass an array of ids to loadMemberData rather than an individual member id at a time, so you only do one query for all of them.
Oh, sorry, forgot about that. Yeah, that's indeed more efficient :)
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

Laloeka

I have another question extending this one, so I could continue here:

I want to check if a user is completely banned, or partially banned (i.e. not allowed to post), and possibly if it's permanent.

Advertisement: