Mam taki oto kod czatu:
getChat.php
  1. <?php
  2. // wysylam naglowki
  3. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); 
  4. header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" ); 
  5. header("Cache-Control: no-cache, must-revalidate" ); 
  6. header("Pragma: no-cache" );
  7. header("Content-Type: text/xml; charset=utf-8");
  8.  
  9. require('inc/database.php');
  10.  
  11. // sprawdzam, czy wiadomosc zostala wyswietlona
  12. if(isset($_POST['message']) && $_POST['message'] != '') {
  13.  
  14. $sql = "INSERT INTO message(chat_id, user_id, user_name, message, akceptacja, post_time) VALUES (" . 
  15. db_input($_GET['chat']) . ", 1, '" . db_input($_POST['name']) . 
  16. "', '" . db_input($_POST['message']) . "', 0, NOW())";
  17. db_query($sql);
  18. }
  19.  
  20. // generuje XML
  21. $xml = '<?xml version="1.0" ?><root>';
  22. //Check to ensure the user is in a chat room.
  23. if(!isset($_GET['chat'])) {
  24. $xml .= '<message id="0">';
  25. $xml .= '<user>Admin</user>';
  26. $xml .= '<text>:)</text>';
  27. $xml .= '<time>' . date('h:i') . '</time>';
  28. $xml .= '</message>';
  29. } else {
  30. $last = (isset($_GET['last']) && $_GET['last'] != '') ? $_GET['last'] : 0;
  31.  
  32. $sql = "SELECT message_id, user_name, message, date_format(post_time, '%H:%i') as post_time" . 
  33. " FROM message WHERE chat_id = " . db_input($_GET['chat']) . " AND message_id > " . $last . " AND akceptacja = 1";
  34. $message_query = db_query($sql);
  35.  
  36. while($message_array = db_fetch_array($message_query)) {
  37. $xml .= '<message id="' . $message_array['message_id'] . '">';
  38. $xml .= '<user>' . htmlspecialchars($message_array['user_name']) . '</user>';
  39. $xml .= '<text>' . htmlspecialchars($message_array['message']) . '</text>';
  40. $xml .= '<time>' . $message_array['post_time'] . '</time>';
  41. $xml .= '</message>';
  42. }
  43. }
  44. $xml .= '</root>';
  45. echo $xml;
  46. ?>


i plik chat.php odpowiedzialny za obróbkę danych z bazy i wyświetlenie wyników na stronie:

  1. <script language="JavaScript" type="text/javascript">
  2. var sendReq = getXmlHttpRequestObject();
  3. var receiveReq = getXmlHttpRequestObject();
  4. var lastMessage = 0;
  5. var mTimer;
  6.  
  7. // inicjacja strony
  8. function startChat() {
  9. // aktywny kursor w miescu pisania
  10. document.getElementById('txt_message').focus();
  11. // pisanie pytania
  12. getChatText();
  13. }
  14.  
  15. // wysylanie do przegladarki XmlHttpRequest Object
  16. function getXmlHttpRequestObject() {
  17. if (window.XMLHttpRequest) {
  18. return new XMLHttpRequest();
  19. } else if(window.ActiveXObject) {
  20. return new ActiveXObject("Microsoft.XMLHTTP");
  21. } else {
  22. document.getElementById('p_status').innerHTML = 'Status: Cound not create XmlHttpRequest Object. Consider upgrading your browser.';
  23. }
  24. }
  25.  
  26. // wysylanie pytania
  27. function getChatText() {
  28. if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
  29. receiveReq.open("GET", 'getChat.php?chat=1&last=' + lastMessage, true);
  30. receiveReq.onreadystatechange = handleReceiveChat;
  31. receiveReq.send(null);
  32. }
  33. }
  34.  
  35. // dodawanie pytania
  36. function sendChatText() {
  37. if(document.getElementById('txt_message').value == '') {
  38. alert("Proszę wpisać pytanie!");
  39. return;
  40. }
  41. if (sendReq.readyState == 4 || sendReq.readyState == 0) {
  42. sendReq.open("POST", 'getChat.php?chat=1&last=' + lastMessage, true);
  43. sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  44. sendReq.onreadystatechange = handleSendChat;
  45. var param = 'message=' + document.getElementById('txt_message').value;
  46. param += '&name=<?php echo $_GET["name"] ?>';
  47. param += '&chat=1';
  48. sendReq.send(param);
  49. document.getElementById('txt_message').value = '';
  50. }
  51. }
  52.  
  53. // "odswiezanie" strony po wyslaniu pytania
  54. function handleSendChat() {
  55. clearInterval(mTimer);
  56. getChatText();
  57. }
  58.  
  59. //Function for handling the return of chat text
  60. function handleReceiveChat() {
  61. if (receiveReq.readyState == 4) {
  62. var chat_div = document.getElementById('div_chat');
  63. var xmldoc = receiveReq.responseXML;
  64. var message_nodes = xmldoc.getElementsByTagName("message");
  65. var n_messages = message_nodes.length
  66. for (i = 0; i < n_messages; i++) {
  67. var user_node = message_nodes[i].getElementsByTagName("user");
  68. var text_node = message_nodes[i].getElementsByTagName("text");
  69. var time_node = message_nodes[i].getElementsByTagName("time");
  70.  
  71. // pokazuje godzine
  72. chat_div.innerHTML += '<font class="chat_time">' + time_node[0].firstChild.nodeValue + '</font> ';
  73.  
  74. // pokazuje nick
  75. chat_div.innerHTML += '<font class="chat_nick">[' + user_node[0].firstChild.nodeValue + ']</font> ';
  76.  
  77. // pokazuje pytanie
  78. chat_div.innerHTML += '<font class="chat_pytanie">' + text_node[0].firstChild.nodeValue + '</font><BR>';
  79.  
  80. chat_div.scrollTop = chat_div.scrollHeight;
  81. lastMessage = (message_nodes[i].getAttribute('id'));
  82. }
  83. // odswieza co 2 sekundy
  84. mTimer = setTimeout('getChatText();',2000);
  85. }
  86. }
  87.  
  88. //This functions handles when the user presses enter. Instead of submitting the form, we
  89. //send a new message to the server and return false.
  90. function blockSubmit() {
  91. sendChatText();
  92. return false;
  93. }
  94.  
  95. //This function handles the response after the page has been refreshed.
  96. function handleResetChat() {
  97. document.getElementById('div_chat').innerHTML = '';
  98. getChatText();
  99. }
  100. </HEAD>
  101. <BODY onload="java script:startChat();">
  102.  
  103. <div id="div_chat" class="oknoGlowne"></div>
  104.  
  105. <div class="oknoUsers"></div>
  106.  
  107. <form id="frmmain" name="frmmain" onsubmit="return blockSubmit();">
  108. <input type="text" id="txt_message" name="txt_message" style="width: 442px;" />
  109. <input type="button" name="btn_send_chat" id="btn_send_chat" value="Wyślij" onclick="java script:sendChatText();" />
  110. <BR>
  111. <input type="button" name="btn_get_chat" id="btn_get_chat" value="Odśwież czat" onclick="java script:getChatText();" />
  112. </form>


Pytanie - jak wyświetlać na bieżąco ilość logowanych użytkowników oraz ich listę?