pixy Posted July 13, 2006 Share Posted July 13, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/14525-showing-users-online-without-oop/ Share on other sites More sharing options...
dptr1988 Posted July 13, 2006 Share Posted July 13, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/14525-showing-users-online-without-oop/#findComment-57549 Share on other sites More sharing options...
akitchin Posted July 13, 2006 Share Posted July 13, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/14525-showing-users-online-without-oop/#findComment-57551 Share on other sites More sharing options...
pixy Posted July 13, 2006 Author Share Posted July 13, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/14525-showing-users-online-without-oop/#findComment-57553 Share on other sites More sharing options...
akitchin Posted July 13, 2006 Share Posted July 13, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/14525-showing-users-online-without-oop/#findComment-57557 Share on other sites More sharing options...
pixy Posted July 13, 2006 Author Share Posted July 13, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/14525-showing-users-online-without-oop/#findComment-57560 Share on other sites More sharing options...
akitchin Posted July 13, 2006 Share Posted July 13, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/14525-showing-users-online-without-oop/#findComment-57565 Share on other sites More sharing options...
pixy Posted July 13, 2006 Author Share Posted July 13, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/14525-showing-users-online-without-oop/#findComment-57566 Share on other sites More sharing options...
akitchin Posted July 13, 2006 Share Posted July 13, 2006 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 Link to comment https://forums.phpfreaks.com/topic/14525-showing-users-online-without-oop/#findComment-57575 Share on other sites More sharing options...
ShogunWarrior Posted July 13, 2006 Share Posted July 13, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/14525-showing-users-online-without-oop/#findComment-57583 Share on other sites More sharing options...
pixy Posted July 13, 2006 Author Share Posted July 13, 2006 ^ 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! Quote Link to comment https://forums.phpfreaks.com/topic/14525-showing-users-online-without-oop/#findComment-57599 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.