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

WBOY
Release: 2016-06-23 13:34:28
Original
983 people have browsed it

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

大神帮忙看下问题出在哪了?


回复讨论(解决方案)

没仔细看。单这一句: session_start();
因其前面有html标签的输出,肯定会有“headers already sent”的错误提示的吧?

你都没说你出什么问题了,让大家如何下手

没仔细看。单这一句: session_start();
因其前面有html标签的输出,肯定会有“headers already sent”的错误提示的吧?

你都没说你出什么问题了,让大家如何下手


 function getMessage(){    //创建一个xmlHttpRequest对象    var myXmlHttpRequest=getXmlHttpObject();    if(myXmlHttpRequest){              var url="getMessageController.php";     var data="getter=<?php echo $loginuser; ?>&sender=<?php echo $username; ?>";           myXmlHttpRequest.open("post",url,true);     myXmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");     myXmlHttpRequest.onreadystatechange=function(){      if(myXmlHttpRequest.readyState==4){        if(myXmlHttpRequest.status==200){          //注释1 window.alert("aaa");          var mesRes=myXmlHttpRequest.responseXML;          var cons=mesRes.getElementsByTagName("con");          var sendtimes=mesRes.getElementsByTagName("sendtime");          //注释2 window.alert("bbb");          if(cons.length!=0){            for(var i=0;i<cons.length;i++){             $("mycons").value+="<?php echo $username;?>说:"+cons[i].childNodes[0].nodeValue+"___"+sendtimes[i].childNodes[0].nodeValue;                                       }          }        }      }     }     myXmlHttpRequest.send(data);    }  }
Copy after login

主要就是回调函数出问题, 注释1处的能响应,但是注释2处的window.alert就无法响应。大神帮忙看下。

书写健壮的代码是很重要的

....if(myXmlHttpRequest.status==200){  var mesRes=myXmlHttpRequest.responseXML;  if(mesRes.xml == '') { //如果不是 xml 对象    alert(myXmlHttpRequest.responseText);//看一下究竟返回了什么    return;  }....
Copy after login
Copy after login

这个估计也就是个外表吧,性能比较差。
聊天室,采用websockeet或comet,
其次也不必用数据库,redis或文件存储即可

书写健壮的代码是很重要的

....if(myXmlHttpRequest.status==200){  var mesRes=myXmlHttpRequest.responseXML;  if(mesRes.xml == '') { //如果不是 xml 对象    alert(myXmlHttpRequest.responseText);//看一下究竟返回了什么    return;  }....
Copy after login
Copy after login

谢谢,已经解决了,怪我粗心了,原来是MessageService.class.php中,第二个header()的分号写成了冒号,晕死啊!

这个估计也就是个外表吧,性能比较差。
聊天室,采用websockeet或comet,
其次也不必用数据库,redis或文件存储即可

嗯,谢谢了。

header("Cache-Control:no-cache"):
这种错误,提示应该很明显吧

谢谢,已经解决了,怪我粗心了,原来是MessageService.class.php中,第二个header()的分号写成了冒号,晕死啊!

我的错误跟你的一样,但是MessageService.class.php并没有使用header()呀??

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