程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> 用PHP+MySql編寫聊天室

用PHP+MySql編寫聊天室

編輯: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>


【本文版權歸作者與奧索網共同擁有,如需轉載,請注明作者及出處】    
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved