Page 1 of 1

ONLINE

Posted: Thu Jun 28, 2007 3:02 am
by shivam0101
Hello,

I want to display current online users lists. How do i go about it?

when a user logs in i inserted '1' into a field, then when he logs out, updated the table with 0 in that field. But, what happens when the user does not log out and simpley closes the browser?

Thanks
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:2. Use descriptive subjects when you start a new thread. Vague titles such as "Help!", "Why?" are misleading and keep you from receiving an answer to your question.

Posted: Thu Jun 28, 2007 3:40 am
by volka
You can update the field in the database with the current timestamp each time the user requests a page.
Then you query all records that have a timestamp that is not older than x minutes. The corresponding users are considered "online".

Posted: Thu Jun 28, 2007 8:14 am
by Gente

Posted: Thu Jun 28, 2007 12:16 pm
by shivam0101
Thanx. But is there any way where i can get the user details once they log in, without refreshing, like gmail chat?

Posted: Thu Jun 28, 2007 12:21 pm
by patrikG
Speak thee, oh mighty forum, for they search-function hath been neglected: viewtopic.php?t=34665

Posted: Fri Jun 29, 2007 1:15 am
by shivam0101
copy pasted the entire code and named it as online.php. Whenever i enter new user name i am able to see their names, but the names does not disappear

Posted: Fri Jun 29, 2007 2:15 am
by patrikG
how about posting that with the tutorial?

Posted: Fri Jun 29, 2007 3:06 am
by shivam0101
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


file name - online.php

Code: Select all

<?php
//require_once("includes/dbConnect.inc.php"); 
//require_once("includes/session.inc.php"); 

mysql_connect('localhost','root','');
mysql_select_db('testsite');

session_start();

if(!isset($_POST['upd'])){ 
    mysql_query("update online set last = now() where userid = ".$_SESSION['loggedinid']) 
        or die("update death ".mysql_error()); 
    $tryupdate = mysql_affected_rows(); 
    if(!$tryupdate != 0){ 
        mysql_query("insert into online 
        ( 
        username, 
        userid, 
        last 
        ) 
        values 
        ( 
        '".mysql_escape_string($_SESSION['loggedin'])."', 
        ".$_SESSION['loggedinid'].", 
        now() 
        )") 
            or die("insert death ".mysql_error()); 
    } // end if for need to insert new row...burrito 
} // end if for post upd set...burrito 
$getnames = mysql_query("select * from online where last > '".date("Y-m-d H:i:s", time() - 1)."' order by username") 
    or die(mysql_error()); 
if(isset($_POST['upd'])){ 
    mysql_query("update online set last = now() where userid = ".$_POST['who']) 
        or die(mysql_error()); 
    $nameslist = ""; 
    while($gtnames = mysql_fetch_assoc($getnames)){ 
        $nameslist .= addslashes($gtnames['username'])."<br>"; 
    } // end while for result set...burrito 
    echo $nameslist; 
    exit; 
} // end if for need to show update...burrito 
?>

Code: Select all

 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
  
<html> 
<head> 
    <title>Online</title> 
<link rel="stylesheet" href="styles/style.css"> 
<script> 
try{ 
    if (window.XMLHttpRequest){ 
        reqsend = new XMLHttpRequest(); 
    }else{ 
        reqsend = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
}catch(e){ 
  
}; 
function updateIt(){ 
    try{ 
        reqsend.open("POST", "online.php", true); 
        var stuff = "upd=1&who=<?=$_SESSION['loggedinid'];?>"; 
  
        reqsend.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
        reqsend.setRequestHeader("Content-Length",stuff.length); 
        reqsend.send(stuff); 
        
        reqsend.onreadystatechange = function(){ 
            if (reqsend.readyState == 4 && reqsend.status == 200) { 
                document.getElementById('namez').innerHTML = reqsend.responseText; 
            } 
        }; 
    }catch(e){ 
        location = "online.php"; 
    } 
    setTimeout("updateIt()",10000); 
} 
</script> 
</head> 
  
<body topmargin="0" leftmargin="0" marginheight="0" marginwidth="0" onLoad="updateIt()"> 
  
<div align="center"> 
<span id="namez"></span> 
</div> 
  
  
</body> 
</html> 



file name: online1.php


Code: Select all

<?php
ob_start();
$textid=$_POST['textid'];
$textname=$_POST['textname'];
if(!empty($textid))
{
	session_start();
	$_SESSION['loggedinid']=$textid;
	$_SESSION['loggedin']=$textname;

	header("Location: online.php");
}

?>

<form action="" method="post">
<table width="53%" border="1">
  <tr>
      <td height="24"><input name="textid" type="text"></td>
	   <td height="24"><input name="textname" type="text"></td>
  </tr>
  <tr>
    <td><input type="submit" name="Submit" value="Submit"></td>
  </tr>
</table>
</form>
<?php
ob_end_flush();
?>

In the database created a table online with userid= int(11) Autoincr, last=varchar(20), username=varchar(10)

in the online1.php i enter id and name


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]