Jump to content

Showing users online without OOP?


pixy

Recommended Posts

I honestly do not understand OOP for my life, so I need to find a way to show users who are logged in without it.

How would I go about doing that? It's my own user log in script I made. It has a bunch of fields, but still the basic username, password, email, etc.

Are there any tutorials out there for doing this? I googled and didn't find what I was looking for.
Link to comment
https://forums.phpfreaks.com/topic/14525-showing-users-online-without-oop/
Share on other sites

there's a discussion about this here:

[url=http://www.phpfreaks.com/forums/index.php/topic,95397.msg381689/topicseen.html#msg381689]http://www.phpfreaks.com/forums/index.php/topic,95397.msg381689/topicseen.html#msg381689[/url]

have a look, and let us know if you're still murky about how to do it.

OOP is merely a different approach to doing a task, it isn't necessary to do any job in particular.  its pros and cons are heavily debated (even on this forum), so let's not start another one here, lest we stray from the actual question.
if you read further, ken goes on to suggest a way of only updating the database every couple of minutes.  set the current time once you've updated it the first time, then check if the current time is 3 (or 4 or 5 or however many) minutes past the session time.  if it is, update the db again; otherwise, just leave it be.  that way for each user you only run an update every few minutes, which is hardly as bad as every page refresh.
you can just use a UNIX timestamp.  update the db, then run:

[code]<?php
$_SESSION['status_updated'] = time();
?>[/code]

then when you want to check whether five minutes has passed, check the session's time against the current time like so:

[code]<?php
$time_difference = time() - $_SESSION['status_updated'];
if ($time_difference > 5*60)
{
  update again, reset $_SESSION['status_updated'] to current time
}
?>[/code]

this is in the case that you want to use 5 minutes.  a UNIX timestamp is measured in seconds, so we compare the time difference to whatever interval we want in seconds.
well if you want to get everyone that has been online in the last 5 minutes, you can use entirely MySQL functions:

[code]SELECT username FROM online_users WHERE time_online > DATE_SUB(NOW(), INTERVAL 5 MINUTE)[/code]

this will grab the username of anyone whose status was updated in the last 5 minutes (by checking if their hypothetical time_online field is more recent [or greater] than 5 minutes ago).

don't forget to change the query to suit however you're storing the users and their time.
I personally think OOP is good for programming style and functionality but it is only a way of coding, it doesn't replace arrays, databases, cookies, sessions etc.

You could have a session variable which will contain functions to do the dirty work, e.g:
[code]
$session = new session;
$session->start();
$logged_in = $session->check_logged_in();
if($logged_in)
{
  echo('Hello, '.$session->var('member_name'));
}
[/code]

Thank kind of thing.
^ I'm already using sessions to store their username.

[quote author=akitchin link=topic=100488.msg396635#msg396635 date=1152823294]
well if you want to get everyone that has been online in the last 5 minutes, you can use entirely MySQL functions:

[code]SELECT username FROM online_users WHERE time_online > DATE_SUB(NOW(), INTERVAL 5 MINUTE)[/code]

this will grab the username of anyone whose status was updated in the last 5 minutes (by checking if their hypothetical time_online field is more recent [or greater] than 5 minutes ago).

don't forget to change the query to suit however you're storing the users and their time.
[/quote]
Thanks, that's just what I was looking for!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.