Advertisement:

Author Topic: SMF check if the user is logged on  (Read 1965 times)

Offline Laloeka

  • Semi-Newbie
  • *
  • Posts: 14
SMF check if the user is logged on
« on: May 05, 2012, 11:05:51 AM »
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!

Offline Yoshi

  • Customizer
  • SMF Hero
  • *
  • Posts: 8,127
  • Gender: Male
Re: SMF check if the user is logged on
« Reply #1 on: May 05, 2012, 11:14:13 AM »
Hi Laloeka, welcome to SMF!

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

If so, do this for every user:
Code: [Select]
loadMemberData(userid);
And don't forget to global $user_profile.

Then, you can use this:
Code: [Select]
$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 / [WIP] Mod Builder / GitHub profile / "A programmer is just a tool which converts caffeine into code."
Quote
<FLAMER> Marketing is about to get into drug activities maybe... but we will see about that later on :P
<Yoshi2889> We're getting free drugs?
<CoreISP> He's talking about caffeine man, damn pen lifter.

Offline Laloeka

  • Semi-Newbie
  • *
  • Posts: 14
Re: SMF check if the user is logged on
« Reply #2 on: May 05, 2012, 11:32:21 AM »
That's great, thanks! :)

Offline Yoshi

  • Customizer
  • SMF Hero
  • *
  • Posts: 8,127
  • Gender: Male
Re: SMF check if the user is logged on
« Reply #3 on: May 05, 2012, 11:37:02 AM »
No problem :)
If your topic is solved, please hit that green button below :)
My Mods / [WIP] Mod Builder / GitHub profile / "A programmer is just a tool which converts caffeine into code."
Quote
<FLAMER> Marketing is about to get into drug activities maybe... but we will see about that later on :P
<Yoshi2889> We're getting free drugs?
<CoreISP> He's talking about caffeine man, damn pen lifter.

Offline Doctor Deejay

  • On Hiatus
  • SMF Hero
  • *
  • Posts: 3,491
  • Gender: Male
Re: SMF check if the user is logged on
« Reply #4 on: May 05, 2012, 12:10:19 PM »
Ehm shouldn't this be in coding discussion? :P
Doctor Deejay                                        I understand everything... except that wig. - Jack Sparrow
Simple Machines LocalizerDon't pm me for support
deejay [at] simplemachines [dot] orgGet To Know The Team - Dr. Deejay

Offline Yoshi

  • Customizer
  • SMF Hero
  • *
  • Posts: 8,127
  • Gender: Male
Re: SMF check if the user is logged on
« Reply #5 on: May 05, 2012, 12:12:44 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 / [WIP] Mod Builder / GitHub profile / "A programmer is just a tool which converts caffeine into code."
Quote
<FLAMER> Marketing is about to get into drug activities maybe... but we will see about that later on :P
<Yoshi2889> We're getting free drugs?
<CoreISP> He's talking about caffeine man, damn pen lifter.

Offline Arantor

  • SMF Legend
  • *
  • Posts: 50,879
    • wedgebook on Facebook
Re: SMF check if the user is logged on
« Reply #6 on: May 05, 2012, 12:15:30 PM »
Quote
If 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...

Offline Yoshi

  • Customizer
  • SMF Hero
  • *
  • Posts: 8,127
  • Gender: Male
Re: SMF check if the user is logged on
« Reply #7 on: May 05, 2012, 12:35:50 PM »
Quote
If 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 / [WIP] Mod Builder / GitHub profile / "A programmer is just a tool which converts caffeine into code."
Quote
<FLAMER> Marketing is about to get into drug activities maybe... but we will see about that later on :P
<Yoshi2889> We're getting free drugs?
<CoreISP> He's talking about caffeine man, damn pen lifter.

Offline Laloeka

  • Semi-Newbie
  • *
  • Posts: 14
Re: SMF check if the user is logged on
« Reply #8 on: August 18, 2012, 07:45:58 PM »
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.