Jump to content


Photo

Showing users online without OOP?


  • Please log in to reply
10 replies to this topic

#1 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 13 July 2006 - 08:00 PM

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.

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#2 dptr1988

dptr1988
  • Members
  • PipPipPip
  • Advanced Member
  • 372 posts

Posted 13 July 2006 - 08:03 PM

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



Need more help with your project? One of the thousands of programmers, web designers or artists at <a href="http://www.rentacode...d_6764522">Rent A Coder</a> would be happy to help.

Disclaimer: Free advice is usually worth what you paid for it. ( or at least when it's coming from me! )

#3 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 13 July 2006 - 08:05 PM

there's a discussion about this here:

http://www.phpfreaks....html#msg381689

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.

#4 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 13 July 2006 - 08:08 PM

See, I really didn't want to have to update the database on every page. Is there any way to do it without querying the database every time?

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#5 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 13 July 2006 - 08:11 PM

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.

#6 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 13 July 2006 - 08:17 PM

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?

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#7 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 13 July 2006 - 08:25 PM

you can just use a UNIX timestamp.  update the db, then run:

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

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

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

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.

#8 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 13 July 2006 - 08:27 PM

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

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

Can you do that?

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#9 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 13 July 2006 - 08:41 PM

well if you want to get everyone that has been online in the last 5 minutes, you can use entirely MySQL functions:

SELECT username FROM online_users WHERE time_online > DATE_SUB(NOW(), INTERVAL 5 MINUTE)

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.

#10 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 13 July 2006 - 08:55 PM

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:
$session = new session;
$session->start();
$logged_in = $session->check_logged_in();
if($logged_in)
{
  echo('Hello, '.$session->var('member_name'));
}

Thank kind of thing.
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#11 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 13 July 2006 - 09:24 PM

^ I'm already using sessions to store their username.

well if you want to get everyone that has been online in the last 5 minutes, you can use entirely MySQL functions:

SELECT username FROM online_users WHERE time_online > DATE_SUB(NOW(), INTERVAL 5 MINUTE)

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.

Thanks, that's just what I was looking for!

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users