By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,730 Members | 1,540 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,730 IT Pros & Developers. It's quick & easy.

Display name of users who are online

P: 26
Hi,
I have made a website wherein the user creates his account. Then the user can login into his account, update his account, search for people with different key skills. Theres also a discussion board and private messaging system in it. I have tried a simple script for live discussion with the help of a tutorial. Its kind of a live open chat. Now i wanted that when the user clicks on the live chat link there should be a frame on that window where the name of users who are online should displayed. If the user either logs out or closes the chat window then his name should be removed from the list. I created a field called Online in userdetails table and giving it a enum value. But i dont know how to create that frame where username will be displayed and using meta tags in it to refresh it every regular interval of time.
This is the code for discussion where i wanted to insert the frame.
Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. session_start(); 
  3.  
  4. $user = $_SESSION['username']; 
  5. ?> 
  6.  
  7. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> 
  8. <html> 
  9. <head> 
  10.    <title>Live Chat</title> 
  11.    <link href="style/style.css" rel="stylesheet" type="text/css" /> 
  12.     <script language="javascript" type="text/javascript"> 
  13.     <!-- 
  14.       var httpObject = null; 
  15.       var link = ""; 
  16.       var timerID = 0; 
  17.       var nickName = "<?php echo $user; ?>"; 
  18.  
  19.       // Get the HTTP Object 
  20.       function getHTTPObject(){ 
  21.          if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP"); 
  22.          else if (window.XMLHttpRequest) return new XMLHttpRequest(); 
  23.          else { 
  24.             alert("Your browser does not support AJAX."); 
  25.             return null; 
  26.          } 
  27.       }    
  28.  
  29.       // Change the value of the outputText field 
  30.       function setOutput(){ 
  31.          if(httpObject.readyState == 4){ 
  32.             var response = httpObject.responseText; 
  33.             var objDiv = document.getElementById("result"); 
  34.             objDiv.innerHTML += response; 
  35.             objDiv.scrollTop = objDiv.scrollHeight; 
  36.             var inpObj = document.getElementById("msg"); 
  37.             inpObj.value = ""; 
  38.             inpObj.focus(); 
  39.          } 
  40.       } 
  41.  
  42.       // Change the value of the outputText field 
  43.       function setAll(){ 
  44.          if(httpObject.readyState == 4){ 
  45.             var response = httpObject.responseText; 
  46.             var objDiv = document.getElementById("result"); 
  47.             objDiv.innerHTML = response; 
  48.             objDiv.scrollTop = objDiv.scrollHeight; 
  49.          } 
  50.       } 
  51.  
  52.       // Implement business logic     
  53.       function doWork(){     
  54.          httpObject = getHTTPObject(); 
  55.          if (httpObject != null) { 
  56.             link = "message.php?nick="+nickName+"&msg="+document.getElementById('msg').value; 
  57.             httpObject.open("GET", link , true); 
  58.             httpObject.onreadystatechange = setOutput; 
  59.             httpObject.send(null); 
  60.          } 
  61.       } 
  62.  
  63.       // Implement business logic     
  64.       function doReload(){     
  65.          httpObject = getHTTPObject(); 
  66.          var randomnumber=Math.floor(Math.random()*10000); 
  67.          if (httpObject != null) { 
  68.             link = "message.php?all=1&rnd="+randomnumber; 
  69.             httpObject.open("GET", link , true); 
  70.             httpObject.onreadystatechange = setAll; 
  71.             httpObject.send(null); 
  72.          } 
  73.       } 
  74.  
  75.       function UpdateTimer() { 
  76.          doReload();    
  77.          timerID = setTimeout("UpdateTimer()", 5000); 
  78.       } 
  79.  
  80.  
  81.       function keypressed(e){ 
  82.          if(e.keyCode=='13'){ 
  83.             doWork(); 
  84.          } 
  85.       } 
  86.     //--> 
  87.     </script>    
  88. </head> 
  89. <body onLoad="UpdateTimer();"> 
  90.     <div id="main"> 
  91.       <div id="icon">&nbsp;</div> 
  92.  
  93.      <div id="result"> 
  94.      <?php  
  95.         $data = file("msg.html"); 
  96.         foreach ($data as $line) { 
  97.             echo $line; 
  98.         } 
  99.      ?> 
  100.       </div> 
  101.       <div id="sender" onKeyUp="keypressed(event);"> 
  102.          Your message: <input type="text" name="msg" size="30" id="msg" /> 
  103.          <button onClick="doWork();">Send</button> 
  104.       </div>    
  105.     </div> 
  106. </body> 
  107.  
Apr 9 '08 #1
Share this Question
Share on Google+
6 Replies


eragon
100+
P: 431
check into Sajax. i have a script for the frame once you install sajax, so contact me afterwards.

Sajax can be found by google.
Apr 11 '08 #2

P: 26
check into Sajax. i have a script for the frame once you install sajax, so contact me afterwards.

Sajax can be found by google.
Thanks.
Ive downloaded SAJAX version 0.12.
Now can you plz help me with displaying name of users at a side on my chat window which refreshes itself to display the users as they log in and log out.
Apr 11 '08 #3

ronverdonk
Expert 2.5K+
P: 4,258
check into Sajax. i have a script for the frame once you install sajax, so contact me afterwards.

Sajax can be found by google.
eragon: please do NOT show fully developed and coded scripts here!

This is a forum for helping people with their own coding or questions, not for supplying complete coded scripts.

moderator
Apr 11 '08 #4

eragon
100+
P: 431
eragon: please do NOT show fully developed and coded scripts here!

This is a forum for helping people with their own coding or questions, not for supplying complete coded scripts.

moderator
thanks for the pointer. i know that, besides, i dont put my finished work up here cause its worse than my handwriting. and thats bad. if i had all my codes given to me i would never be as skilled as i was now.

--------------------------------- Now the how-to: -------------------------------

  1. Include a text file (named atv.txt for simplicity) this stores the online users.
  2. check if the array_key_exists and we use the key $tol (time online) and we check against the username string. if it doesnt exist set the value of $tol['username'] to time().
  3. set another string, $content to the opening PHP statement (<?php) and put a line break. run a foreach loop in which all values of $tol are read as $name and $time.
  4. inside the loop append the PHP syntax to define the array piece in a string as if.... easier shown than told at this point.

    [PHP]$content .= "\$tol['".$name."'] = $time; \n";[/PHP]

    then end the loop.
  5. Appeend "?>" to the end of $content and write it to atv.txt.



    ------------------------------ Part 2 ------------------------------------
  6. unset all strings used but the username string. include your updated file once more.
  7. run the same foreach loop as before, but inside put a code that checks: if the time logged plus 5 (or any offset) is less than the value of time(), unset $tol['thatuser'].
  8. repeat steps 3-5.



    ---------------------------- Part 3 ----------------------------------------
  9. unset the same strings and once more include your updated file.
  10. set a new string called $output and its starting value is "Active users: <br>",
  11. do the same foreach loop, but inside the loop make it simply echo the username and a <br> tag.
  12. close the loop and return $function.



    Now we get to work on the SAJAX part....

    ------------------------ Part 4 (allmost done) -----------------------
  13. add the following to your php code:



    [PHP]$sajax_request_type = "GET";

    sajax_init();

    sajax_export("show_now");

    sajax_handle_client_request();[/PHP]



    and finally end the code. Start the HTML for your page, but freeze before you close your head section.
  14. open javascript and in your first line add PHP that calls the function sajax_show_javascript(). MAKE SURE this function is in php tags.
  15. create a javascript function show_users(user_list) { ...
  16. in which case you find the element by id userlist and set it's inner html to the var user_list.
  17. make another function called get_list(param) and inside it put a JAVASCRIPT call to the function x_show_now(show_users);
  18. in the function, set a timeout that calls the function again (thus resulting in a loop) and set it for 5 seconds. end the function. end javascript.



    ---------------- Part 5 (final touch) -------------------------------
  19. in your <body> tag put onLoad="get_list()"
  20. finally, where you want your user list to show up, simply make a DIV with the ID userlist.
Thank you and good night. If you get stuck ( i expect it ) contact me or anyone else here at TSDN... sorry, BDN.

(there, i didnt post my source code)
Apr 12 '08 #5

P: 26
Thankyou. I tried doing using frames with meta refresh.
I tried the frame part.
I made another file called chat.php code for which is as follows
Expand|Select|Wrap|Line Numbers
  1. <HTML>
  2. <HEAD>
  3. <TITLE> Live Chat </TITLE>
  4. </HEAD>
  5. <FRAMESET COLS="70%,*"> 
  6. <FRAME SRC="index.php"> 
  7. <FRAME SRC="online.php">
  8. <meta http-equiv="refresh" content="5"/> 
  9. </FRAMESET>
  10. <noframes>
  11. </noframes> 
  12. </HTML>
  13.  
In the online.php file following code is there
Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. echo "Online Users";
  3. $query = mysql_query("SELECT UserName FROM userdetails WHERE Online = '1'") or die(mysql_error());
  4. while($row = mysql_fetch_array($query))
  5. {
  6. echo $row['UserName']; //display users
  7. ?>
  8.  
Now the problem is it doesnt get refresh automaticaly. If we refresh it manually then it displays the name of users who are online and also removes the names of users who have logged out. I think the problem is in meta refresh tags that i have used. They are not placed properly. Also the name of the users are displayed in the same line. Each name is not displayed on a new line. Plz help me with it.
Apr 13 '08 #6

P: 26
The problem of refreshing got solved. I misplaced the meta tags now its working correctly. I wanted to display the name,logo of the site and the menu option on top of chat so how can i do that. If i place them directly on top of frames then the frames get misplaced. I tried using even another frame for it but that too didnt worked out. Can you plz help me with placing the following layout in my chat.php file.
Expand|Select|Wrap|Line Numbers
  1. <body>
  2. <table width="700" border="0" cellspacing="0" cellpadding="0">
  3.   <tr>
  4.     <td height="24" bordercolor="#FFFFFF" bgcolor="#F7EEDF"><img src="images/header1.GIF" width="700" height="26" /> </td>
  5.   </tr>
  6. </table>
  7. <table width="700" border="0" cellspacing="0" cellpadding="0">
  8.   <tr>
  9.     <td width="192" height="140" bgcolor="#0000000"><img src="images/logo2.JPG" width="259" height="178" /></td>
  10.     <td width="508" bgcolor="#FFFFFF"><img src="images/search.gif" width="441" height="198" /></td>
  11.   </tr>
  12. </table>
  13. <table width="700" border="25" cellpadding="0" cellspacing="0">
  14.   <tr>
  15.     <td width="106" height="30" align="center" valign="middle" bgcolor="#620000" class="style8"><a href="../loggedin.php" class="navigation">Home</a></td>
  16.     <td width="90" align="center" valign="middle" bgcolor="#620000"><span class="style8"><a href="../UAboutUs.html" class="navigation"> About Us</a></span></td>
  17.     <td width="122" align="center" valign="middle" bgcolor="#620000"><span class="style8"><a href="../account.php" class="navigation">Check Account</a></span></td>
  18.     <td width="106" align="center" valign="middle" bgcolor="#620000" class="style8"><a href="../search.php" class="navigation">Search</a></span></td>
  19.     <td width="106" align="center" valign="middle" bgcolor="#620000" class="style8"><a href="../UFAQs.html" class="navigation">FAQs</a></td>
  20.     <td width="108" align="center" valign="middle" bgcolor="#620000" class="style8"><a href="../UContactUs.html" class="navigation">Contact Us</a> </td>
  21.   </tr>
  22. </table>
  23.  
Plz let me know.
Apr 13 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.