Jump to content

[solved] online now?


pro_se

Recommended Posts

hiya! i am working on a web community and the last project i have to tackle is the online now image. i was wondering if anyone in this genius bucket knows how to create a mysql/ php session online now thing. okay now some backend information -- the session is set when a user logs in, this session is called $_SESSION['username'];. also, when a user logs in a session variable gets set to 1 (meaning logged in) this variable is called $logged_in... hope thats enough information and all your input is greatly appreciated!
Link to comment
Share on other sites

I've done this before.. 

#1 thing you need to have a time stamp saved for every user logged in..  I generally do this in the members table in a field called last_activity

#2 if You want to calculate how many guests are online -- you might want to make another table called "Online_Guests" with 1 field with the name of "Username"

#3 -- how long do you want it delayed? and do you want to be able to change it?  If you want to change it then you'd be better off doing a database setup on that part and put say 15 in the delay for 15 minutes..  Once you have that -- this would be your basic code..

[CODE]
<?php
$time = time() - 60 * 15;
$mems = "SELECT * FROM members WHERE last_activity <= $time";
$mems = mysql_num_rows(mysql_query($mems), 0);
$guest = "SELECT * FROM online_guests";
$guest = mysql_num_rows(mysql_query($guest), 0);
$tot = "SELECT * FROM members";
$tot = mysql_num_rows(mysql_query($tot), 0);
echo $mems." members online";
echo $guest." guests online";
echo $tot." total members";
?>
[/CODE]

Keep in mind i just threw that together -- it may have a few bugs but for the most part it could work..  you could also instead of using the mysql_num_rows command use the count(*) in the select query instead of just *..  Hope it has helped.. have fun...
Link to comment
Share on other sites

ahh you want a list of names for those who are logged in.. okay how bout this code...

[CODE]
<?php
$time = time() - 60 * 15;
$mems = "SELECT * FROM members WHERE last_activity <= $time";
$mems = mysql_query($mems);
$cnt = mysql_num_rows($mems);
for ($i = 0; $i < $cnt && $rows = mysql_fetch_assoc($mems); $i++) {
echo $rows['username'].', ';
}
?>
[/CODE]

Something similar to this may be what you are looking for... Now also keep in mind I have not tested this and it may not work as planned but it should get you on your way to what you want...

Breakdown of the code:

Select * from database where last activity is 15 minutes ago or after..

$cnt - count how many entries are in the results...

for loop -- i = 0 and is less than $cnt lets get an associative array and up it to the next info...

echo $rows['username'].', ' will simply print out "Radar, "


Link to comment
Share on other sites

Well you should already have a table that you have all your members listed in.. just add a field at the end called last_activity -- and then on every page have it update their last activity by using the mysql command UPDATE..

Then to get the time for the update just use $time = time();  this will give you a series of digits in seconds...  seconds are easier to work with...

Once that is done, you impliment the code above wherever you want it to show on your site.
Link to comment
Share on other sites

time() gets you the ammount in seconds for the current date and time...  - 60 means minus 60 seconds.. and * 15 means 60 * 15 which is 15 minutes.. so basically $time = time() - 60 * 15; just makes the time it is now minus 15 minutes...
Link to comment
Share on other sites

its on a local server so i will attach it... the sql dump: CREATE TABLE `users` (
  `status` varchar(255) NOT NULL default 'working',
  `rel` varchar(255) NOT NULL default '',
  `orient` varchar(255) NOT NULL default '',
  `gend` varchar(255) NOT NULL default '',
  `id` int(10) NOT NULL auto_increment,
  `name` varchar(20) NOT NULL default '',
  `username` varchar(40) default NULL,
  `password` varchar(50) default NULL,
  `regdate` varchar(20) default NULL,
  `email` varchar(100) default NULL,
  `website` varchar(150) default NULL,
  `location` varchar(150) default NULL,
  `last_login` varchar(20) default NULL,
  `fronttext` text NOT NULL,
  `headline` varchar(50) NOT NULL default 'This is my Headline',
  `counter` varchar(200) NOT NULL default '1',
  `avatar` varchar(60) NOT NULL default 'images/user/nopic.gif',
  `hobbies` varchar(255) NOT NULL default '',
  `music` varchar(255) NOT NULL default '',
  `reading` varchar(255) NOT NULL default '',
  `heroes` varchar(255) NOT NULL default '',
  `mstatus` varchar(255) NOT NULL default '',
  `inout` varchar(50) NOT NULL default 'out',
  `fname` varchar(255) NOT NULL default '',
  `lname` varchar(255) NOT NULL default '',
  `showdetails` char(1) NOT NULL default 'n',
  `showgeneral` char(1) NOT NULL default 'n',
  `last_activity` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`id`)
)

[attachment deleted by admin]
Link to comment
Share on other sites

yeah this is gonna be easier.. much much much easier.. lol

First off -- you'll need to be able to have a way to read the user of the profile or whatever..  some sort of a string that the username the online/offline image is going to show up under...  in the instance of my code imma use $user_name
[CODE]
<?php
$time = time() - 15 * 60
$mem = "SELECT inout FROM users WHERE username = '$user_name'";
$mem = mysql_query($mem);
$mem = mysql_result($mem, 0);
if ($mem == "in") {
echo '<img src="in.gif">';
} else {
echo '<img src="out.gif">';
}
?>
[/CODE]

This should be more at what you are looking for...  The code before was trying to list all of their usernames.. lol...
Link to comment
Share on other sites

okay so lets do it like this instead...

<?php
$time = time() - 15 * 60
$mem = "SELECT last_activity FROM users WHERE username = '$user_name'";
$mem = mysql_query($mem);
$mem = mysql_result($mem, 0);
if ($time => $mem) {
echo '<img src="in.gif">';
} else {
echo '<img src="out.gif">';
}
?>

Then as said before you would use the UPDATE query command to update the last_activity with $time = time() ... this seem more like what you want?
Link to comment
Share on other sites

Okay try changing the query I gave you to this (both lines of $mem other than the result)..

[CODE]
$mem = "SELECT last_activity FROM users WHERE username = '$user_name'";
$mem = mysql_query($mem) or die("Error: ". mysql_error(). " with query ". $mem); // what's wrong?
[/CODE]

change it to that -- run it again and then tell me what it says...
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.