网站地图    收藏   

主页 > php专栏 > php综合实列 >

ajax php 聊天室实例代码(4) - 综合实例

来源:自学PHP网    时间:2014-12-02 13:09 作者: 阅读:次

[导读] htmlheadtitle迷你php+ajax聊天室演示?phpecho$title;? titlemetahttp-equiv= 39;pragma 39;content= 39;no-cache 39; me...

ajax php 聊天室实例代码(4)

  1. <html> 
  2. <head> 
  3.  <title>迷你php+ajax聊天室演示 <?php echo $title;?></title> 
  4.  <meta http-equiv='pragma' content='no-cache' /> 
  5.  <meta http-equiv=content-type content="text/html; charset=<?php echo $charset;?>" /> 
  6.  <meta name="keywords" content="<?php echo $lang["keywords"];?>"> 
  7.  <meta name="description" content="mini ajax chatroom by longbill. <?php echo $description;?>"> 
  8. <style type='text/css'> 
  9. body { text-align:center; color:#333333; font-size:12px; font-family:宋体;} 
  10. a { text-decoration:none; color:#a2b700; } 
  11. .mydiv { text-align:left; margin:5px; padding:5px; border:1px solid #ff8c05; background-color:#fdd283; width:600px; } 
  12. .inputtext { border:0px; border-bottom:1px solid #333333; background-color:transparent;} 
  13. .submit { border:1px solid #ff8c05; background-color:transparent; } 
  14. .contents { border:1px solid #ff8c05;margin:5px; margin-top:10px;background-color:#ffffff; overflow:auto;word-break:break-all;word-wrap :break-word;} 
  15. .bg { background-color:#ffffff; } 
  16. .content { border:0px;background-color:transparent;width:auto; font-size:16px; font-family:fixedsys; margin:2px; padding:1px; } 
  17. .time { color:#aaaaaa; font-size:10px; font-family:arial;} 
  18. .online { margin:5px; padding:0px; display:inline; } 
  19. .mybut { width:20px; height:20px; background-color:#ff8c05; text-align:center; font-size:18px; color: #333333;} 
  20. </style> 
  21. <script> 
  22. function $(obj) 
  23. { 
  24.  return document.getelementbyid(obj); 
  25. } 
  26. function setcookie(name,value,t) 
  27. { 
  28.  var cookieexp = 5*30*24*60*60*1000; //5 months 
  29.  var cookiestr=name+"="+escape(value)+";"; 
  30.  var expires = ""; 
  31.  var d = new date(); 
  32.  var t2=(!t)?cookieexp:t*60*1000; 
  33.  d.settime( d.gettime() + cookieexp); 
  34.  expires = "expires=" + d.togmtstring()+";"; 
  35.  document.cookie = cookiestr+ expires; 
  36. } 
  37. function getcookie(name) 
  38. { 
  39.  var start = document.cookie.indexof( name + "=" ); 
  40.  var len = start + name.length + 1; 
  41.  if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) return ""; 
  42.  if ( start == -1 ) return ""; 
  43.  var end = document.cookie.indexof( ";", len ); 
  44.  if ( end == -1 ) end = document.cookie.length; 
  45.  return unescape( document.cookie.substring( len, end ) ); 
  46. } 
  47. function createajax() 
  48. { 
  49.  if (window.xmlhttprequest)  
  50.  { 
  51.   var ohttp = new xmlhttprequest(); 
  52.   return ohttp; 
  53.  }  
  54.  else if (window.activexobject)  
  55.  { 
  56.   var versions = [ 
  57.    "msxml2.xmlhttp.6.0", 
  58.    "msxml2.xmlhttp.3.0" 
  59.   ]; 
  60.   for (var i = 0; i < versions.length; i++)  
  61.   { 
  62.    try { 
  63.     var ohttp = new activexobject(versions[i]); 
  64.     return ohttp; 
  65.    } catch (error) {} 
  66.   } 
  67.      } 
  68.  throw new error("your browser doesn't support xmlhttprequest"); 
  69. } 
  70. function pickcolor() 
  71. { 
  72.  if (!window.isie) return; 
  73.  var scolor = $('dlghelper').choosecolordlg(); 
  74.  var color = scolor.tostring(16); 
  75.  while (color.length<6) color="0"+color; 
  76.  window.color = color; 
  77.  color = "#"+color; 
  78.  $('div_color').style.backgroundcolor = color; 
  79.  $('div_color').value = color; 
  80. } 
  81. var isie = (document.all && window.activexobject) ? true : false; 
  82. </script> 
  83. </head> 
  84. <body > 
  85. <center> 
  86. <div class=mydiv style='text-align:center; border:0px; background-color:transparent; font-size:25px; color:#ff8c05;'><?php echo $title;?></div> 
  87. <div class="mydiv login" id='div_description'> 
  88. <?php echo $description;?> 
  89. </div> 
  90. <div class="mydiv rooms" id='div_msg'> 
  91. <div class='contents' style='height:350px;' id='div_contents'>loading...</div> 
  92. </div> 
  93. <div class="mydiv login" id='div_name' style='display:block;'> 
  94. name:<input type=text class="inputtext bg" size=8 id='chat_user' value='' maxlength=30 />&nbsp; 
  95. <object id=dlghelper classid="clsid:3050f819-98b5-11cf-bb82-00aa00bdce0b" width="0px" height="0px"></object> 
  96. <input class="inputtext" style='width:50px;cursor:hand;10px;background-color:#000000;color:#ffffff;' id='div_color' onclick="pickcolor()" value="#000000" onblur="this.style.backgroundcolor=this.value;window.color=this.value.replace('#','');" /> 
  97. &nbsp;size:<input class="inputtext bg" type=text style='width:20px' maxlength=3 id='input_size' value='16' />(px) 
  98. &nbsp;font:<select id='input_font' class='inputtext bg' style='width:70px;'> 
  99. <option value='fixedsys'>fixedsys</option> 
  100. <option value='heiti'>黑体</option> 
  101. <option value='songti'>宋体</option> 
  102. <option value='kaiti'>楷体</option> 
  103. </select> 
  104. bold:<input type=checkbox id='input_bold' class='inputtext' style='border-bottom:0px;' /> 
  105. window:<a class='mybut' href='#' onclick='resize(1)'>+</a> 
  106. &nbsp;<a class='mybut' href='#' onclick='resize(0)'>-</a> 
  107. &nbsp;<a class='mybut' style='width:25px;font-size:16px;' href='#' onclick='clearall()'>clear</a> 
  108. </div> 
  109. <div class="mydiv login" id='div_word'> 
  110. <textarea type=text class="inputtext bg" rows=1 scrolling=no style='height:20px;overflow:hidden;width:500px;' id='chat_word' onfocus="if (this.value == '<?php echo $lang["hereyourwords"];?>') this.value=''; window.editing=0; "  
  111.  onkeydown="return check_send(event);" ><?php echo $lang["hereyourwords"];?></textarea> 
  112. <input type=button class=submit value='send' onclick="chat_send();$('chat_word').style.height=20;" onfocus="this.blur();"/> 
  113. </div> 
  114. <div class='mydiv' style='display:<?php if (!$disonline) echo "none";?>' id='div_online'>loading online...</div> 
  115. <script> 
  116. var debug = 0; 
  117. var lastmod = <?php echo time()-$earlier*60;?>; 
  118. var login = 1; 
  119. var loading = false; 
  120. var olduser = getcookie('chatusername'); 
  121. if (olduser != "") $('chat_user').value = olduser; 
  122. var room = "<?php echo $room;?>"; 
  123. var first = 1; 
  124. var dis = "<?php echo $least;?>"; 
  125. var lastword; 
  126. var color=''; 
  127. var touchs = <?php echo $touchs;?>; 
  128. var dotouch = true; 
  129. var maxdisplay = <?php echo $maxdisplay;?>; 
  130. var nowdisplay = 1; 
  131. var sending = 0; 
  132. var loaded_lines = []; 
  133. var editing = 0; 
  134. function encode(s) 
  135. { 
  136.  return  (encodeuricomponent)? encodeuricomponent(s):s; 
  137. } 
  138. $('chat_user').onfocus = setonfocus; 
  139. $('input_size').onfocus = setonfocus; 
  140. function setonfocus() 
  141. { 
  142.  window.editing = 1; 
  143. } 
  144. function setonblur() 
  145. { 
  146.  window.editing = 0; 
  147. } 
  148. var keep_ajax; 
  149. function keeponline() 
  150. { 
  151.  var name = $('chat_user').value; 
  152.  if (!name) return; 
  153.  keep_ajax = createajax(); 
  154.  keep_ajax.open('post','<?php echo basename(__file__);?>',1); 
  155.  keep_ajax.setrequestheader("content-type", "application/x-www-form-urlencoded"); 
  156.   
  157.  keep_ajax.onreadystatechange = function () 
  158.  { 
  159.   if (keep_ajax.readystate == 4 && keep_ajax.status == 200) 
  160.   { 
  161.    //alert(keep_ajax.responsetext); 
  162.   } 
  163.  } 
  164.  keep_ajax.send("action=keep&name="+encode(name)); 
  165. } 
  166. setinterval("keeponline()",touchs*1000); 
  167. function quitroom() 
  168. { 
  169.  if(confirm("你真的要离开聊天室吗?")) 
  170.  { 
  171.   var ajax = createajax(); 
  172.   ajax.open('post','<?php echo basename(__file__);?>',0); 
  173.   ajax.setrequestheader("content-type", "application/x-www-form-urlencoded"); 
  174.   ajax.send("action=quit&name="+encode($('chat_user').value)); 
  175.   //alert("sending close  action=quit&name="+encode($('chat_user').value)); 
  176.   //alert("response:"+ajax.responsetext); 
  177.  } 
  178.  else return ''; 
  179. } 
  180. document.body.onbeforeunload =  quitroom; 
  181. setinterval(" load_word()",(debug)?6000:1000); 
  182. var load_word_ajax; 
  183. //下载完成后的处理函数 
  184. function load_word_change() 
  185. { 
  186.  if (load_word_ajax.readystate == 4) 
  187.  { 
  188.   if (load_word_ajax.status != 200) 
  189.   { 
  190.    load_word_error(); 
  191.    return; 
  192.   } 
  193.   window.loading = false; 
  194.   var body = $('div_contents');  
  195.   try { 
  196.    if (debug) alert(load_word_ajax.responsetext); 
  197.    eval("var arr = "+load_word_ajax.responsetext);  
  198.   } catch(e) 
  199.   { 
  200.    alert('error 101njson syntax error!nn'+load_word_ajax.responsetext); 
  201.    return; 
  202.   } 
  203.   if (!arr || !arr.lastmod || typeof(arr.lastmod) == "undefined" ) 
  204.   { 
  205.    return; 
  206.   } 
  207.   var html = ""; 
  208.   var line = arr.lines; 
  209.   var i = 0; 
  210.   var v1 = 0; 
  211.   var div_online = $('div_online'); 
  212.   if (window.first) 
  213.   { 
  214.    body.innerhtml = ""; 
  215.    window.first = false; 
  216.   } 
  217.    
  218.   if (arr.onlines) 
  219.   { 
  220.    $('div_online').innerhtml = ""; 
  221.    for(var i=0;i<arr.onlines.length;i++) addonline(arr.onlines[i]); 
  222.   } 
  223.   for(var i=0;i<line.length;i++) 
  224.   { 
  225.    var linekey = line[i].word.substring(line[i].word.length-20,line[i].word.length)+line[i].time; 
  226.    if (window.loaded_lines[linekey] === true) 
  227.    { 
  228.     if (debug) alert("jump:"+linekey); 
  229.     continue; 
  230.    } 
  231.    var div1 = document.createelement("div"); 
  232.    window.nowdisplay ++; 
  233.    if (window.nowdisplay > window.maxdisplay) window.nowdisplay = 1; 
  234.    if ($("contentitem"+window.nowdisplay)) body.removechild($("contentitem"+window.nowdisplay)); 
  235.    div1.classname = "content"; 
  236.    div1.id = "contentitem"+window.nowdisplay; 
  237.    div1.innerhtml = line[i].word+" <span class='time'>("+line[i].time+")</span>"; 
  238.    body.appendchild(div1); 
  239.     
  240.    window.loaded_lines[linekey] = true; 
  241.    body.scrolltop = 655350; 
  242.    v1 = 1; 
  243.   }  
  244.   if (v1)  
  245.   { 
  246.    window.focus();  
  247.    document.body.focus(); 
  248.    window.lastmod = arr.lastmod; 
  249.    if(debug) alert("lastmod = "+arr.lastmod + " nwindow.lastmod="+window.lastmod); 
  250.    if ($('chat_word').disabled == false && window.editing != 1) 
  251.    { 
  252.     $('chat_word').focus(); 
  253.    } 
  254.   } 
  255.  } 
  256. } 
  257. function load_word_error() 
  258. { 
  259.  window.loading = false; 
  260.  window.status = 'error 102:while loading words'; 
  261.  settimeout("window.status = '';",5000); 
  262. } 
  263. function load_word() 
  264. { 
  265.  load_word_ajax = createajax(); 
  266.  if (window.loading) 
  267.  { 
  268.   try 
  269.   { 
  270.    load_word_ajax.abort(); 
  271.    window.loading = false; 
  272.   }catch(e) {} 
  273.  } 
  274.  if (!window.lastmod) 
  275.  { 
  276.   alert("window.lastmod="+window.lastmod); 
  277.   return; 
  278.  } 
  279.   
  280.  load_word_ajax.open('post','<?php echo basename(__file__);?>',true); 
  281.  load_word_ajax.onreadystatechange = load_word_change; 
  282.   
  283.  var urlstring = ''; 
  284.  urlstring += "lastmod="+window.lastmod; 
  285.  urlstring+= "&room="+room; 
  286.  urlstring+= "&action=read"; 
  287.  urlstring+= "&name="+encode($('chat_user').value); 
  288.   
  289.  if (window.first) 
  290.  { 
  291.   urlstring+= "&first=true"; 
  292.   urlstring += "&dis="+dis; 
  293.  } 
  294.  //如果到了取得在线用户的时间 
  295.  if (window.dotouch)  
  296.  { 
  297.   urlstring+= "&touchme=true"; 
  298.   window.dotouch = false; 
  299.   //垃圾内存回收 
  300.   try { collectgarbage(); } catch(e) {} 
  301.  } 
  302.  window.loading = true; 
  303.  if (debug) alert("sending:"+urlstring); 
  304.  load_word_ajax.setrequestheader("content-type", "application/x-www-form-urlencoded"); 
  305.   
  306.  load_word_ajax.send(urlstring); 
  307. } 
  308. function touchme() 
  309. { 
  310.  window.dotouch = true; 
  311.  settimeout("touchme()",window.touchs*1000); 
  312. } 
  313. function showalert(a,n) 
  314. { 
  315.  if (!n) n=0; 
  316.  if (n>3) return; 
  317.  if (!a) 
  318.  { 
  319.   a = 0; 
  320.   b = 1; 
  321.  } 
  322.  else 
  323.  { 
  324.   a = 1; 
  325.   b = 0; 
  326.  } 
  327.  document.title = mytitle[a]; 
  328.  settimeout("showalert("+b+","+(n+1)+");",500); 
  329. } 

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论