Hello, I have developed a login system for my script that uses sessions and cookies. I was wondering if anyone could suggest me how forums, social networking sites, and etc., tell when users are logged in? It seems as if a user is inactive long enough, their status will no longer be logged in (session still exists, though). I'm not sure how it could be done, though. Any suggestions? Thanks!
you need to setup one table ONLINE with fields ip, user_id, user_name, lasttime for every user you need to check if the IP is in the list $sql = "SELECT ip FROM online WHERE ip = '" . $_SERVER['REMOTE_ADDR'] . "' LIMIT 1"; $query = mysql_query($sql); $items = mysql_num_rows($query); Code (markup): if $items == 0 Insert one into the database (check for sessions so you can add name, otherwise name it guest and user_id = 0) if $items == 1 Update row with last timestamp and check if user is logged in/out/whatever and update that to After each check you need to DELETE items from the table who are older then xxx time and that can be done with something like DELETE FROM online WHERE UNIX_TIMESTAMP(lastdate) < (NOW() - 60*15) // 15 minuten Code (markup): Hope this helps you!
I made this script some while ago.. Works perfect: <? //Test if user is online/offline $databaseTime = $getUserInfo[isonline]; //Timestamp from database. $currentTime = time(); //Current unix time. $timeDiff = $currentTime - $databaseTime; // Time between current time and database time. //If the time diff is < 1790, the user is online. if($timeDiff < 1790){ $isOnline = 1; //Declare variable. }else{ $isonline = 0; //Declare variable. } if($isOnline == 1){ //User is online. ?> <div class="statusshowonline"> <b><? echo $showuser[online]; ?></b> </div> <? }else{ //User is offline. ?> <div class="statusshowoffline"> <b><? echo $showuser[offline]; ?></b> </div> <? } ?> PHP: You should have this code on top of each page: $time_start = time(); //Unix time.. mysql_query("UPDATE users SET isonline = $time_start WHERE username = '".$_SESSION['username']."'"); PHP: