模仿韩顺平老师做的web聊天系统,大神帮忙看下出了啥有关问题

WBOY
Release: 2016-06-13 12:20:13
Original
811 people have browsed it

模仿韩顺平老师做的web聊天系统,大神帮忙看下出了啥问题

本帖最后由 alakey 于 2014-01-18 22:52:59 编辑
<br />*******chatRoom.php******<br /><html><br /><head><br /><?php<br />//接受open传递的用户名<br /> $username=$_GET['username'];<br /> $username=trim($username);<br /> session_start();<br /> $loginuser=$_SESSION['loginuser'];<br />?><br /><meta http-equiv="content-type" content="text/html;charset=utf-8"/><br /><script type="text/javascript" src="my.js"></script><br /><script type="text/javascript"><br /><br />  window.resizeTo(800,700);<br />  window.setInterval("getMessage()",3000);<br /><br />   function getMessage(){<br />    //创建一个xmlHttpRequest对象<br />    var myXmlHttpRequest=getXmlHttpObject();<br />	if(myXmlHttpRequest){<br />		<br />	 var url="getMessageController.php";<br />	 var data="getter=<?php echo $loginuser; ?>&sender=<?php echo $username; ?>";<br />	 <br />     myXmlHttpRequest.open("post",url,true);<br />	 myXmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");<br />     myXmlHttpRequest.onreadystatechange=function(){<br />	  if(myXmlHttpRequest.readyState==4){<br />	    if(myXmlHttpRequest.status==200){<br />		  var mesRes=myXmlHttpRequest.responseXML;<br />		  var cons=mesRes.getElementsByTagName("con");<br />		  var sendtimes=mesRes.getElementsByTagName("sendtime");<br />		  if(cons.length!=0){<br />		    for(var i=0;i<cons.length;i++){<br />			 $("mycons").value+="<?php echo $username;?>说:"+cons[i].childNodes[0].nodeValue+"___"+sendtimes[i].childNodes[0].nodeValue;<br />			<br />             <br />			}<br />		  }<br />		}<br />	  }<br />	 }<br />     myXmlHttpRequest.send(data);<br />	}<br />  }<br />  function sendMessage(){<br />  //创建一个xmlHttpRequest对象<br />    var myXmlHttpRequest=getXmlHttpObject();<br />	if(myXmlHttpRequest){<br />		<br />	 var url="sendMessageController.php";<br />	 var data="con="+$('con').value+"&getter=<?php echo $username; ?>&sender=<?php echo $loginuser; ?>";<br />	 <br />     myXmlHttpRequest.open("post",url,true);<br />	 myXmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");<br />     myXmlHttpRequest.onreadystatechange=function(){<br />	   if(myXmlHttpRequest.readyState==4){<br />	     if(myXmlHttpRequest.status==200){<br />		   //这里是返回信息,暂不需要<br />		   <br />		 }<br />	   <br />	   }<br />	 <br />	  }<br />       myXmlHttpRequest.send(data);<br />	}<br />  }<br /><br /></script><br /></head><br /><body><br /><center><br /><br /><h1>网页聊天室(<font color="blue"><?php echo $loginuser; ?></font>正在和<font color="red"><?php echo $username; ?></font>聊天)</h1><br /><textarea cols="60" rows="20" id="mycons"></textarea><br/><br /><input type="text" style="width:300px" id="con"/><br /><input type="button" value="发送" onclick="sendMessage()"/><br /></center><br /></body><br /></html><br /><br />*********getMessageController.php********<br /><br /><?php<br />require_once 'MessageService.class.php';<br />header("content-type:text/xml;charset=utf-8");<br />header("Cache-Control:no-cache"):<br /> $getter=$_POST['getter'];<br /> $sender=$_POST['sender'];<br /> <br /> $messageService=new MessageService();<br /><br /> $messageList=$messageService->getMessage($getter,$sender);<br /> <br /> echo $messageList;<br /><br /> ?><br /><br />*********MessageService.class.php********<br /><br /><?php<br /><br />require_once 'SqlHelper.class.php';<br />class MessageService{<br /><br /> function addMessage($sender,$getter,$con){<br />$sql="insert into messages (sender,getter,content,sendtime) values ('$sender','$getter','$con',now())";<br />$sqlHelper=new SqlHelper();<br />return $sqlHelper->execute_dml($sql);<br />}<br /><br /> function getMessage($getter,$sender){<br /> <br />    $sql="select * from messages where getter='$getter' and sender='$sender' and isGet=0";<br />    <br />    $sqlHelper=new SqlHelper();<br /><br />	$array=$sqlHelper->execute_dql2($sql);<br />    	$messageInfo="<meses>";<br />    for($i=0;$i<count($array);$i++){<br />	  $row=$array[$i];<br />      $messageInfo.="<mesid>{$row['id']}</mesid><sender>{$row['sender']}</sender><getter>{$row['getter']}</getter><con>{$row['content']}</con><sendtime>{$row['sendtime']}</sendtime>";<br />	}<br />     $messageInfo.="</meses>";<br />	 $sqlHelper->close_connect();<br />     return $messageInfo;<br /> }<br /><br />}<br />?><br />
Copy after login

大神帮忙看下问题出在哪了?
------解决思路----------------------
没仔细看。单这一句: session_start();
因其前面有html标签的输出,肯定会有“headers already sent”的错误提示的吧?

你都没说你出什么问题了,让大家如何下手
------解决思路----------------------
书写健壮的代码是很重要的
....<br />if(myXmlHttpRequest.status==200){<br />  var mesRes=myXmlHttpRequest.responseXML;<br />  if(mesRes.xml == '') { //如果不是 xml 对象<br />    alert(myXmlHttpRequest.responseText);//看一下究竟返回了什么<br />    return;<br />  }<br />....<br />
Copy after login

------解决思路----------------------
这个估计也就是个外表吧,性能比较差。
聊天室,采用websockeet或comet,
其次也不必用数据库,redis或文件存储即可
------解决思路----------------------
header("Cache-Control:no-cache"):
这种错误,提示应该很明显吧
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template