Jump to content

Archived

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

pixy

Showing users online without OOP?

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.

Share this post


Link to post
Share on other sites
I honestly do not understand why people even use OOP  ;)

It should be easy to do it without OOP. If you post the important parts of your existing script, I might be able to help


Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
See, I really didn't want to have to update the database on every page. Is there [b]any way[/b] to do it without querying the database every time?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Can someone explain detecting session time to me?
Would I just create a variable...
$_SESSION['time'] = NOW();
and then...
How do I add 5 mintes onto NOW() to see if it's been that long?

Share this post


Link to post
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.

Share this post


Link to post
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?

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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!

Share this post


Link to post
Share on other sites

×

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.