vForums Support > Programming & Coding :: Programming Discussion :: > On Site Check

On Site Check - Posted By dog199200 (dog199200) on 10th Feb 10 at 1:28pm
Here is a question, how do you run a check to see if a person is on a website?

The reason I ask is because i've been coding an IM system that stores whether the person is online inside of a database rather then a cookie cause i kept having problems with the cookie. The thing is though it will still say they are online within the database even when they are offline, and because of this is it still showing them in the online list. If i know of a way I could check to see if they where on the website, i could use the check in an if statement, and so when they leave the site it updates the database, changing it from online to offline. Well here is the code i got so far:


Code:
 
  1. $session_check = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
  2. mysql_select_db(DB_NAME) or die(mysql_error());
  3.  
  4. $userid = getUserID();
  5.  
  6. if (!$_SESSION['userid']) {
  7. mysql_query("UPDATE cometchat_status SET status='offline' WHERE userid='$userid' ");
  8. }
  9.  
  10. mysql_close($session_check);
 



Re: On Site Check - Posted By Michael (wrighty) on 10th Feb 10 at 1:35pm
Everytime they reload the page you set a database item perhaps called Last_online to something like time(); (current unix timestamp)

Then you do a query to get people that have time()-Last_online < 900 (aka 15 mins) ... that'll show people that were last active in the last 15 mins. That is, that they've navigated around the site in the last 15 mins. {Smile}

Re: On Site Check - Posted By dog199200 (dog199200) on 10th Feb 10 at 1:38pm
ok and then if they weren't around in the last 15 minutes, i can use that in an if statement and update the database. Thanks i think i can do that {Smile}

Re: On Site Check - Posted By Michael (wrighty) on 10th Feb 10 at 1:40pm
You don't have to update the database. 'cause you shouldn't really have an online/offline field! {Unsure}

Re: On Site Check - Posted By dog199200 (dog199200) on 10th Feb 10 at 1:55pm
but i do need the field XD a lot of things are calling from the fields. The IM system i am doing right now is for a friend, i'm setting one up so it will integrate with his vbulletin. But when you mentioned the time with the last active, i realized i could use vb's last active table, which makes this rather easy

Re: On Site Check - Posted By Michael (wrighty) on 10th Feb 10 at 2:18pm
Yes. No point having 2 fields being used when the one (last active) will suffice! {Smile}

Re: On Site Check - Posted By dog199200 (dog199200) on 10th Feb 10 at 2:51pm
yea but i can't seem to get it to work XD I have never used the time() function before and i did look it up, and did what you said and here is what i came up with:

Code:
 
  1. $session_check = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
  2. mysql_select_db(DB_NAME) or die(mysql_error());
  3.  
  4. $userid = getUserID();
  5. $last_online = ("SELECT lastvisit FROM user WHERE userid = '".$userid."'");
  6.  
  7. if (time() - $last_online < 5) {
  8. mysql_query("UPDATE cometchat_status SET status='offline' WHERE userid='".$userid."'");
  9. }
  10.  
  11. mysql_close($session_check);
 



$userid = getUserID(); <------------------------ Gets The user Id

$last_online = ("SELECT lastvisit FROM user WHERE userid = '".$userid."'"); <--------------------- Calls the array for the active to the propers userid


if (time() - $last_online < 5) { <-------------- the if statement running the time() function
mysql_query("UPDATE cometchat_status SET status='offline' WHERE userid='".$userid."'"); <---- what happens if the IF returns true.
}




The code as is does nothing at all, I tihnk i have done everything right. I also flipped the < before the 5 into an > and it just always says the person is logged out. I knowi'm always asking for help, but I don't ask unless i have been trying for a while.

Re: On Site Check - Posted By Michael (wrighty) on 10th Feb 10 at 3:34pm
5 means 5 seconds. Now I doubt it'll do anything. Try something big to test! {Smile}

Re: On Site Check - Posted By dog199200 (dog199200) on 10th Feb 10 at 5:43pm
i have XD i set it to 60 seconds, it didn't work either then i tried 5 minutes (300), and yet still did nothing

Re: On Site Check - Posted By Michael (wrighty) on 10th Feb 10 at 6:01pm
$last_online = ("SELECT lastvisit FROM user WHERE userid = '".$userid."'");

Won't work ... you'll have to run that through as a query (mysql_query(....)) {Smile}

Re: On Site Check - Posted By dog199200 (dog199200) on 10th Feb 10 at 6:05pm
oh yea, i left it out because of the rest of the code was being called


Edit: Still not helping any