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
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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

So how would I query the database to show members online?

[code]
<?php
$time = time() > 5*60;
$query = "SELECT username FROM users WHERE (online > '$time')";
$result = mysql_query($query) or mysql_error();
?>
[/code]

Can you do that?
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

^ 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!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.