Whos Online - Cookies?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ANYang
Forum Newbie
Posts: 5
Joined: Fri May 20, 2005 12:42 am

Whos Online - Cookies?

Post by ANYang »

I need help creating a who's online script in php for my site. I have found a few who's online script but I can't/don't know how to incoporate it into my site.

Just like the who's online script on vbulletin and phpbb, when a user logs in. I want it to display the username online.

Here's the cookie setting for the users.

Code: Select all

// REMEMBER ME

if(isset($rememberme) AND $rememberme == &quote;1&quote;) {
setcookie(&quote;username&quote;, &quote;$user&quote;, time()+60*999999, &quote;/&quote;);
setcookie(&quote;password&quote;, &quote;$pass&quote;, time()+60*999999, &quote;/&quote;);
setcookie(&quote;u_id&quote;, &quote;$u_id&quote;, time()+60*999999, &quote;/&quote;);
} else {
// DONT REMEMBER ME
setcookie(&quote;username&quote;, &quote;$user&quote;, 0, &quote;/&quote;);
setcookie(&quote;password&quote;, &quote;$pass&quote;, 0, &quote;/&quote;);
setcookie(&quote;u_id&quote;, &quote;$u_id&quote;, 0, &quote;/&quote;);
}

Code: Select all

<?
$is_logged_in = "no";
if(isset($_COOKIE['username']) & isset($_COOKIE['password']) & isset($_COOKIE['u_id'])) {
$u_id = u_decrypt($_COOKIE['u_id']);
$user_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_users WHERE u_id='$u_id'"));
if(stripslashes($_COOKIE['username']) == u_encrypt($user_info[username]) & stripslashes($_COOKIE['password']) == $user_info[password]) {
$is_logged_in = "yes";
} else {
$is_logged_in = "no";
}
} else {
$is_logged_in = "no";
}

if($is_logged_in == "yes") {
echo "You are logged in as $user_info[username].<br />";
} else {
include("form.php");
}
?>
I've read the "whos online" post here and it is all about using sessions as the log in. In the script I'm trying to create a whos online for is using cookie.[quote][/quote]
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

i dont use cookies or sessions, i use a database with 4 fields,

ID || username || ip || last_action

Code: Select all

function AddUpdateWHOIS ();
{
   // run initial query to get all array's    
   // write code to collect everything from that database
   $query = "SELECT ID from whois_table WHERE ip ='{$array['ip']}'";
   $result = mysql_query ($query) or die (mysql_error());
   // check if there was already a match and if not this is a new user
   $num_rows = mysql_num_rows($result); 
   if ($num_rows === 1)
   {
      // user has already been listed
      // update last active time so they dont fall out of 'focus'
      // UPDATE QUERY HERE
   }
   else
   {
      // user is a new visitor
      // INSERT QUERY HERE
   }
}

function DeleteWHOIS ($time_from)
{
   $query = "DELETE FROM whois_table WHERE time =< '{$time_NOW}'";
   $result = mysql_query ($query) or die (mysql_error());
}


function GetWHOIS ($time_from)
{
   // this query wqill automatically get all the users online listed in the DB
   $query = "SELECT * FROM whois_table WHERE time >= '{$time_from}'";
   $result = mysql_query ($query) or die (mysql_error());
   $num_rows = mysql_num_rows($result);
   if ($num_rows >= 1)
   {
      // theres results, gather array
      while ($array = mysql_fetch_array($result));
      {
         // do the output
      }
   }
   else
   {
      // no-one is online (Should never happen as the user viewing this page is but i like to keep this in just for readability
   }
}
at the beginning of each page run

Code: Select all

$time_from = (60 * 15); // 60 seconds  15 == 15 Minutes
DeleteWHOIS ($time_from);
AddUpdateWHOIS ();
// run this where you want your output in your page
GetWHOIS ($time_from);
I actually have the whole thing written with extra's that i use as standard in quite a few of my projects, unfortunately i am not on my home machine right now
Last edited by malcolmboston on Fri May 20, 2005 5:35 am, edited 2 times in total.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

i corrected some parse errors in the code :wink:
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

lol, yeah sorry, boss walked in, boss doesnt like me writing code for others :cry:

EDIT: i fixed some stuff u missed
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

Post by Archy »

I would personally add the delete function into a cron job, and run it every hour. However, it would really depend on how active your site was.
ANYang
Forum Newbie
Posts: 5
Joined: Fri May 20, 2005 12:42 am

Post by ANYang »

I believe the code above shows the # of ppl that is online at the moment. What I really want is to show the username of the ppl that are logged in. I'm running a blog hosting site using 'bloghoster'. I believe bloghoster doesn't doesn't use session instead they uses cookies. I'm having a hard time incoprating this with a whos online that shows the name of the ppl that are logged in.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

you could still do what malcom suggested.

1)set a cookie for when they log in
2)update a field when they "move around" the site
3)query the db on page loads to see who's "moved around" in the last 5 (or howver long you want) minutes
4)display who's still "online"

I recently did something that's a little more advanced and requires the user to have javascript enabled, but it's much more "realtime" (I check who's online for the past 30 seconds) and it updates automatically (without page reloads) so if I'm just sitting on a page and not moving for 5 minutes but 3 ppl have dropped off, It would reflect that dynamcially (xmlhttp...it's your friend, in fact if it were a living entity, I think I'd marry it).

again though, it wont' work if user's aren't using JS (but I don't worry about that crowd).
ANYang
Forum Newbie
Posts: 5
Joined: Fri May 20, 2005 12:42 am

Post by ANYang »

:(, I have no clue how to get this working. I need major help...
1)set a cookie for when they log in
2)update a field when they "move around" the site
3)query the db on page loads to see who's "moved around" in the last 5 (or howver long you want) minutes
4)display who's still "online"
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Wow...

Ok here goes.

Table structure (hypothetical)

Code: Select all

id           int(5) auto_increment
username     varchar(20)
pass         varchar(32)
ip           varchar(15)
lastactive   varchar(10)

primary key (id)
1. User logs in
2. Run PHP time() function to get timestamp
3. Update the lastactive column with the timestamp
4. User changes page or submits something etc etc
-> Go back to step 2 etc etc.

Then to find all user's active in past 5 mins for example.

Code: Select all

$scale = 60 * 5; //Secs * mins
$timenow = time();
$time5mins_ago = $timenow - $scale;

$query = "SELECT `username` FROM `users` WHERE `lastactive` > '$time5mins_ago'";
Post Reply