欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

用PHP+MySql编写聊天室

程序员文章站 2022-10-05 22:37:45
大家好!好久没有看到大家了,上次写了用php的通过文件操作的聊天室,当然是漏洞百出的,而且每一次刷新屏幕都很糟糕的!我想了好久,是不是能有一种方法不要刷新,就能取得发言的信...
大家好!好久没有看到大家了,上次写了用php的通过文件操作的聊天室,当然是漏洞百出的,而且每一次刷新屏幕都很糟糕的!我想了好久,是不是能有一种方法不要刷新,就能取得发言的信息了?多用一个框架就能实现这个效果了,即多用一个框架我们称为getmsg,我们让getmeg去取得信息而且保证每一次取得的信息是最新的,就是上次取得的发言就不要了,上次我编的就是每一次取得信息都是重复的,这样效果不好。
     我的设计思想是:每一次发言,都把它提交到服务器上,然后保存到一个数据库里,每格几秒钟getmsg就去服务器上取得 最新发言数据,并保证上次取得的发言就不要取下来了,然后把取下来的发言叠加到一个专门显示发言的窗口(listmsg)去。这样我们就感觉listmsg窗口不会有重刷新的感觉而且,只是看到发言信息不断加进去,这样就有很好的效果,
怎样在一个框架把信息加到另个框架去了?用javascript就可以实现这个想法的:
<script language="javascript">
<!--
var win=parent.frames[0] //第一个框架即在文档中第一次出现的<frame>
var doc=win.document //获取文档
doc.write("发言信息")//这样就可以把信息加到另个frame里去了!
-->
</script>
当然你得在mysql里建两个表,一个表用于保存发言信息,一个表用于保存几个在线的,在列在线人数时,我们让它每个60秒刷新一次,并把一分钟以前的发言删掉,并看某个发言人是不是很长时间没发言了,是的话,就删掉他,
由于表的子段很简单,在这里就不要写出来了,看程序就会知道了。这个程序在网上网下测试的效果都很好!
表的名字是:chat和chat_getmsg
<!--主框架chat.php-->
<?php
if(!isset($username))  
    $username="guest";
  $conid=mysql_connect("localhost","yourcounter","password");
  mysql_select_db("yourdadabase",$conid);
$dstr=date("ymdhis");
$sql="insert chat_getmsg (username,shijian) values ('$username','$dstr')";
mysql_query($sql,$conid);
$sql="update chat_getmsg set shijian='$dstr' where username='$username'";
mysql_query($sql,$conid);
mysql_close($conid);
?>
<html>
<head>
<title>untitled document</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
</head>
<frameset rows="283,105" frameborder="yes" border="1" framespacing="0" cols="*">  
  <frameset cols="520,113" frameborder="yes" border="1" framespacing="0" rows="*">  
    <frame name="topframe" scrolling="auto" border="1" noresize src="list.html" >
    <frame name="rightframe" scrolling="auto" border="1" noresize src="username.php">
  </frameset>
  <frameset rows="17,90" frameborder="yes" border="1" framespacing="0" cols="*">  
   <?php echo "<frame name="topframe1" scrolling="no" border="1" noresize src="getmsg1.php?username=$username">n"; ?>
   <?php echo "<frame name="bottomframe" scrolling="no" border="1" noresize src="fayan.php?username=$username">n"; ?>
  </frameset>
</frameset>
<noframes><body bgcolor="#ffffff">

</body></noframes>
</html>
<!--发言fayan.php -->
<?php
if(!isset($username))
     $username="guest";
if(!isset($yanse))
      $yanse="blue";
if(!isset($objectname))
      $objectname="大家";
?>
<html>
<head>
<title>untitled document</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
.normalfont {  font-family: "宋体"; font-size: 10pt; font-weight: 800; color: #99ff33; height: auto; width: auto; left: 0px; top: 0px; clip:  rect(   )}
-->
</style>
</head>

<body bgcolor="#ff3333">
<form method="post" name="fayanform" action=<?php echo ""fayan.php?username=$username&yanse=$yanse&objectname=$objectname "";?> class="normalfont">
  <table width="100%" border="0" cellspacing="0" cellpadding="0" class="normalfont">
    <tr>  
      <td width="17%"  height="22"> 颜色:  
        <select id=yanse name="yanse">
          <option value="blue" <?php if($yanse=="blue") echo "selected";?> >blue  
          <option value="red"  <?php if($yanse=="red") echo "selected";?> >red
          <option value="green" <?php if($yanse=="green") echo "selected";?>  >green
          <option value="yellow" <?php if($yanse=="yellow") echo "selected";?> >yellow
          <option value="brown" <?php if($yanse=="brown") echo "selected";?> >brown
          <option value="#ff00ff" <?php if($yanse=="#ff00ff") echo "selected";?> >ff00ff
          <option value="#bb00ff" <?php if($yanse=="#bb00ff") echo "selected";?> >bb00ff
          <option value="#cc00ff" <?php if($yanse=="#cc00ff") echo "selected";?> >cc00ff
          <option value="#aa00ff" <?php if($yanse=="#aa00ff") echo "selected";?> >aa00ff
          <option value="#6600ff" <?php if($yanse=="#6600ff") echo "selected";?> >6600ff
          <option value="#7700ff" <?php if($yanse=="#7700ff") echo "selected";?> >7700ff
          <option value="#1100ff" <?php if($yanse=="#1100ff") echo "selected";?> >1100ff
          <option value="#11ffff" <?php if($yanse=="#11ffff") echo "selected";?> >11ffff
          <option value="#11aaff" <?php if($yanse=="#11aaff") echo "selected";?> >11aaff
          <option value="#1199ff" <?php if($yanse=="#1199ff") echo "selected";?> >1199ff
          <option value="#1133ff" <?php if($yanse=="#1133ff") echo "selected";?> >1133ff
        </select>
      </td>
      <td width="55%"  height="22" class="normalfont">发言:  
        <input type="text" name="fayan" size=35 >

        <input type="hidden" name="username" value=<?php echo ""$username""; ?> >

        <input type="button" name="button" value="发言" onclick="submitfayan()">
      </td>
      <td width="28%"  height="22"> </td>
    </tr>
    <tr>  
      <td width="17%" height="20">对象:
        <input type="text" name="objectname" size=10 value=<?php echo ""$objectname"";?> >
      </td>
      <td width="55%"  height="20" class="normalfont"><a href="http://chenlipan.oso.com.cn">回去</a> </td>
      <td width="28%" height="20" class="normalfont" > </td>
    </tr>
  </table>
</form>
</body>
<script language="javascript">
<!--
fayanform.fayan.focus()
function submitfayan()
{
if(fayanform.fayan.value!="")
    fayanform.submit()
else
alert("不能发空信息")
fayanform.fayan.focus()
}
-->
</script>
</html>
<?php
if(isset($username)&&isset($fayan)){
$conid=mysql_connect("localhost","yourcount","yourpassword");
mysql_select_db("database",$conid);
$sql="insert chat (username,objectname,action,msg,color) values ("$username","$objectname","no","$fayan","$yanse")";
mysql_query($sql,$conid);
mysql_close($conid);
}
?>
<!--获取发言信息getmsg.php-->
<html>
<head>
<title>untitled document</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<?php  
$conid=mysql_pconnect("localhost","yourcount","password");
mysql_select_db("database",$conid);

if(!isset($username))
     $username="getmsg";  
$dt=time();   
$newdate=date("ymdhis",$dt);
$fromdate=date("ymdhis",$dt-6);;

$sql="select username as name,msg,action,objectname ,shijian,color from chat where shijian>="$fromdate"";
$res=mysql_query($sql,$conid);

echo "<meta http-equiv="refresh" content="6,url=getmsg.php?username=$username" >n";
echo "</head>n";

echo "<script language="javascript">n";
echo "<!--n";
echo "var win=parent.frames[0]n";
echo "var doc=win.documentn";
echo "var divlin=doc.body.all("lin")n";

while(list($name,$msg,$action,$objectname,$shijian,$color)=mysql_fetch_row($res))
{
if($objectname!="大家"||$objectname) $object="对 <a href=javascript:selectusername('$objectname')>$objectname</a> ";
echo  "divlin.innerhtml=divlin.innerhtml+"<font color=$color size=3><a href=javascript:selectusername('$name')>$name</a> $object 说: $msg </font><br>"n";
}

echo "win.scrollby(6000,6000)n";
echo "-->n";
echo "</script>n";
?>
<body bgcolor="#333333">
</body>

</html>
<!--欢迎词getmsg1.php -->
<html>
<head>
<title>untitled document</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<?php  
if(!isset($username))
     $username="guest";
echo "<meta http-equiv="refresh" content="6;url=getmsg.php?username=$username" >n";
echo "</head>n";

echo "<script language="javascript">n";
echo "<!--hiden";
echo "var win=parent.frames[0]n";
echo "var doc=win.documentn";
echo "var divlin=doc.body.all("lin")n";
echo "divlin.innerhtml= "<font color=blue><b>欢迎 $username 的光临</b></font><br>"n";
echo "-->n";
echo "</script>n";
?>
<body bgcolor="#333333">
</body>
</html>
<!--显示发言list.html-->
<html>
<head>
<title>untitled document</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
.lin {  font-size: 10pt; color: #ff6633}
-->
</style>
</head>
<body bgcolor="#ffffff" >
<center>
<div color=blue>
<font color=white size=6><b>
欢迎光临</b>
</marquee>
</div>
</center>
<div id=lin class="lin"></div>
</body>
<script language="javascript">
<!--
function selectusername(str)
{
var win=parent.frames[3]
var doc=win.document
var form1=doc.body.all("fayanform")
form1.objectname.value=str
}
-->
</script>
</html>

<!--罗列在线人数-->
<html>
<head>
<title>untitled document</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<meta http-equiv="refresh" content="60">
<style type="text/css">
<!--
.lin {  font-size: 10pt; color: #ff0066}
-->
</style>
</head>

<body bgcolor="#33ff00" >
<center class="lin"><b>
  在线人数 </b>
<hr>
<?php
if(!isset($username))
    $username="guest";
$dt=time();   
$newdate=date("ymdhis",$dt);
$fromdate=date("ymdhis",$dt-200);
$linkid=mysql_connect("localhost","yourcount","password");
mysql_select_db("yourdatabase",$linkid);
$sql="update chat_getmsg set shijian="$newdate" where username="$username"";
mysql_query($sql,$linkid);
$sql="select username from chat_getmsg where shijian>="$fromdate"";
$res=mysql_query($sql,$linkid);  
while(list($username)=mysql_fetch_row($res)){
     echo "<a href="javascript:selectusername('$username')">$username</a><br>n";
$fromdate=date("ymdhis",$dt-100);
$sql="delete from chat where shijian<="$fromdate"";
mysql_query($sql,$linkid);
mysql_close($linkid);
}
?>
</center>
<script language="javascript">
<!--
function selectusername(str)
{
var win=parent.frames[3]
var doc=win.document
var form1=doc.body.all("fayanform")
form1.objectname.value=str
}
-->
</script>
</body>
</html>


【本文版权归作者与奥索网共同拥有,如需转载,请注明作者及出处】