Writing chat room _php base with Php+mysql

Source: Internet
Author: User
Hello everyone! Long time no see everyone, the last time I wrote a file with PHP chat room operation, of course, is porous, and every time the screen is very bad! I think for a long time, is there a way not to refresh, you can get the message of the speech? The effect is achieved by using a single frame, that is to use a framework we call getmsg, we let getmeg to obtain information and ensure that every time the information is the latest, is the last to get the speech is not, the last I made up is every time to obtain information is repeated, so the effect is not good.
My idea of design is: every time I speak, it is submitted to the server, and then saved to a database, every few seconds getmsg to the server to obtain the latest data, and to ensure that the last statement was not taken down, and then put the statements taken down to a special display of the Statement of the window ( LISTMSG) go. So we feel that the Listmsg window will not have a heavy refresh feeling and, just to see the message added constantly, this will have a good effect,
How do you add information to another frame in one frame? The idea can be achieved with javascript:
<script language= "JavaScript" >
<!--
var win=parent.frames[0]//First frame that appears in the document for the first time <frame>
var doc=win.document//Get Document
Doc.write ("Speech information")//So you can add information to another frame!
-->
</script>
Of course, you have to build two tables in MySQL, a table for saving speech information, a table is used to save a few online, when the number of people on the line, we let it refresh every 60 seconds, and a minute before the statement deleted, and see if a speaker is not a long time did not speak, yes, then delete him,
Because the child sections of the table are very simple, do not write here, look at the program will know. This program in the network under the test results are very good!
The name of the table is: Chat and chat_getmsg
<!--main frame 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);
?>
<title>untitled document</title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<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>
<!--speaking fayan.php-->
<?php
if (!isset ($username))
$username = "Guest";
if (!isset ($yanse))
$yanse = "Blue";
if (!isset ($objectname))
$objectname = "Everyone";
?>
<title>untitled document</title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<style type= "Text/css" >
<!--
. normalfont {font-family: "Song body"; font-size:10pt font-weight:800; color: #99FF33; height:auto; width:auto; left:0px; top:0px; Clip:rect ()}
-->
</style>

<body bgcolor= "#ff3333" >
<form method= "POST" name= "Fayanform" ACTION=&LT;? PHP echo "" Fayan.php?username= $username &yanse= $yanse &objectname= $objectname "";? > class= "Normalfont" >
<table width= "100%" border= "0" cellspacing= "0" cellpadding= "0" class= "Normalfont" >
<tr>
&LT;TD width= "17%" height= ">" Color:
<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";? > &GT;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>
&LT;TD width= "55%" height= "class=" > spoke:
<input type= "text" name= "Fayan" size=35 >

<input type= "hidden" name= "username" VALUE=&LT;? PHP echo "$username";?> >

<input type= "button" name= "button" value= "Speak" onclick= "Submitfayan ()" >
</td>
&LT;TD width= "28%" height= "> </td>"
</tr>
<tr>
&LT;TD width= "17%" height= ">" Objects:
<input type= "text" name= "objectname" size=10 value=<? PHP echo "" $objectname "";? > >
</td>
&LT;TD width= "55%" height= "class=" Normalfont ><a "href=" > Go back </a> http://chenlipan.oso.com.cn Td>
&LT;TD width= "28%" height= "class=" > </td>
</tr>
</table>
</form>
</body>
<script language= "JavaScript" >
<!--
Fayanform.fayan.focus ()
function Submitfayan ()
{
if (fayanform.fayan.value!= "")
Fayanform.submit ()
Else
Alert ("Cannot send empty information")
Fayanform.fayan.focus ()
}
-->
</script>
? 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);
}
?>
<!--access to information getmsg.php-->
<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 "
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!= "Everyone" | | $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 said: $msg </font><br> "n";
}

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

<!--welcome words getmsg1.php-->
<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 "
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> welcome $username's presence </b></font><br> "n";
echo "-->n";
echo "</script>n";
?>
<body bgcolor= "#333333" >
</body>
<!--Show Speaking list.html-->
<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>
<body bgcolor= "#FFFFFF" >
<center>
<div color=blue>
<font Color=white size=6><b>
Welcome to </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>

<!--list Number online-->
<title>untitled document</title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<meta http-equiv= "Refresh" content= ">"
<style type= "Text/css" >
<!--
. lin {font-size:10pt; color: #ff0066}
-->
</style>

<body bgcolor= "#33ff00" >
<center class= "Lin" ><b>
Online number </b>
? 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>


"This article copyright belongs to the author and the Osso net jointly owns, if needs to reprint, please indicate the author and the origin"
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.