Jump to content

How can you make a "who's online list"?


reportingsjr

Recommended Posts

I know how to create a list that says "7 guests online" and such and it goes up everytime someone visits, but how do you make it go back down when someone leaves? I realize you can make a logout page or a leave site page but in my case people will always be just hitting the close button or going to another website. How can I make it go up and down according to how many people are at your website? I know of a site that does this (uiv2.com, scroll to the bottom and there will be a lin k that says X users online, click it and you will see a list) but I cant figure out how to do it...I really want to know how this is done!!

Link to comment
Share on other sites

True, I shoulda googled it. Ill check that out aspnetguy. Thank you!EDIT: read it.. Wow, shoulda realized how simple it is lol. Kinda what I wanted, but it only shows users that have gone to the page/refreshed it in the past 5 minutes. Thats okay I guess.. Thank you!

Link to comment
Share on other sites

Ah no, check out my site (link in siggy) and scroll to the bottom.What it is, is that you insert the ip and time that the site was viewed at,then you do a select query for any time that was in the past X minutes. Heres my code for my counter and visit thing now:

$query= "DELETE FROM `counter` WHERE `ip` = '{$_SERVER['REMOTE_ADDR']}'";$result=mysql_query($query);$time = time();$query= "INSERT INTO `counter` VALUES ('NULL', '{$_SERVER['REMOTE_ADDR']}', '{$time}')";$result= mysql_query($query);$query= "SELECT * FROM `counter` ORDER BY `number` DESC LIMIT 1";$result = mysql_query($query);$row = mysql_fetch_object($result);echo "This page has been viewed {$row->number} times.<br />";$maxtime = time() - (60 * 10); $query = "SELECT * FROM `counter`    WHERE `timeviewed` >= $maxtime";$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());$num = "0";if(mysql_num_rows($result) > 0){while($row = mysql_fetch_row($result)){ $num++;}}else{echo "No one on this site currently!";}if($num >= "2"){echo "{$num} people are online or have visited the site in the past 10 minutes.";}else{echo "{$num} person is online or has visited the site in the past 10 minutes.";}

Now if you go to my site and scroll to the bottom you will see what it says :)Thanks aspnetguy!!

Link to comment
Share on other sites

A minor optimization to your code - instead of:

$query = "SELECT * FROM `counter`   WHERE `timeviewed` >= $maxtime";$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());$num = "0";if(mysql_num_rows($result) > 0){while($row = mysql_fetch_row($result)){$num++;}

You could have this instead:

$query = "SELECT COUNT(*) FROM `counter`   WHERE `timeviewed` >= $maxtime";$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());$resultArray = mysql_fetch_array($result);$num = $resultArray[0];}

It won't matter much as long as the database and server are on the same computer, but it could save you alot of network traffic otherwise. :)

Link to comment
Share on other sites

um, you really need to do something about your site...the first time it said something about a login, then a fewimages and a little more text, the second time, there was just a little text, and the third time, just the background.

Link to comment
Share on other sites

...reportingsjr, you should use ajax to update your chat box, not JS!!!ajax works by talking to the server though js, but it doesn't refresh the page to update content... unlike the js comand to refresh the page, which fereshes the page...it's really annoying.

Link to comment
Share on other sites

...reportingsjr, you should use ajax to update your chat box, not JS!!!ajax works by talking to the server though js, but it doesn't refresh the page to update content... unlike the js comand to refresh the page, which fereshes the page...it's really annoying.
hey dan do you have ajax on your server?
Link to comment
Share on other sites

Ajax is not "on a server". Ajax is several technologies that you use together for a certain purpose. Specifically, you use javascript, XML (the J and the X in Ajax), and the document object model to send/receive requests without loading the page. It doesn't require the server to support anything unusual, but it does depend on the browser's document object model.

Link to comment
Share on other sites

you kidding me.. If you would look at the script right above where the chat is you would see this:<scipt href="includes/ajax.js" language="javascript"></script>Lol, maybe that will hit you. little goat, what exactly do you mean?

Link to comment
Share on other sites

well, I'm in opera, and all your webpage shows for me is the background.the source looks fine, but it won't show anything. maybe it's just me. it might have something to do with your comments; in a few spots you have one dash (-) instead of two, such as at the end of a few comments.I actually don't know...EDIT: it also might have to do with the fact that some of the attributes in the html tags are escaped (ex. \' ).

Link to comment
Share on other sites

Yea littlegoat, it doesnt work in IE either, says some stupid error like doucment.getElementById is null or not an object.. but I cant fix it from where im at :).

Link to comment
Share on other sites

You can see javascript errors in Opera by going to Tools -> Advanced -> Error Console. From one of the dropdowns you can choose to only show Javascript errors. They are very informative, even give you the line and position where the error happened. If you have the web developer toolbar, there are 2 additional javascript shells under the Utils menu.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...