How would I go about creating an 'Online Now' function?

Discussion in 'PHP' started by Crayz, Nov 30, 2008.

  1. #1
    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!
     
    Crayz, Nov 30, 2008 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    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! :)
     
    EricBruggema, Dec 1, 2008 IP
  3. blueleaf

    blueleaf Active Member

    Messages:
    112
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    I found some code in hotscripts.com earlier. You can check that in the php section.
     
    blueleaf, Dec 1, 2008 IP
  4. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #4
    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:
     
    elias_sorensen, Dec 1, 2008 IP