Use PHP + MYSQL to write chat rooms

Source: Internet
Author: User

Hello everyone! I haven't seen anyone for a long time. The last time I wrote a PHP chat room through file operations, of course, it was full of loopholes, and every time I refreshed the screen, it was terrible! I have been thinking for a long time. Is there a way to get the information of the speech without refreshing it? You can use one framework to achieve this effect. Specifically, you can use a framework called getmsg to obtain information and ensure that the information obtained at each time is the latest, that is, the last time I made the speech, the last time I edited it, the information I obtained was duplicated. This is not a good result.
My design idea is: submit each speech to the server and save it to a database. getmsg will go to the server to obtain the latest speech data every few seconds, make sure that the previous speech is not removed, and then overwrite the obtained speech to a specific display window (listmsg. In this way, we don't feel that the listmsg window will refresh again, but we can see that the speech information is constantly added, which has a very good effect,
How can I add information to another framework in one framework? This idea can be implemented using JavaScript:
<Script language = "JavaScript">
<! --
VaR win = parent. Frames [0] // the first frame that appears for the first time in the document <frame>
VaR doc=win.doc ument // obtain the document
Doc. Write ("") // you can add the information to another frame!
-->
</SCRIPT>
Of course, you have to create two tables in MySQL. One is used to save the speech information, and the other is used to save several online tables. When the number of online users is listed, We need to refresh them every 60 seconds, delete the speech one minute ago and check whether a speaker has not spoken for a long time. If so, delete the speaker,
Because the sub-sections of the table are very simple, do not write them here. Program You will know. This program works well on the Internet!
The table name is chat and chat_getmsg.
<! -- Main framework 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 = "everyone ";
?>
<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>
& Lt; TD width = "17%" Height = "22" & gt; 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 = "#11 ffff" <? PHP if ($ Yanse = "#11 ffff") echo "selected";?> > 11 FFFF
<Option value = "#11 aaff" <? PHP if ($ Yanse = "#11 aaff") echo "selected";?> > 11 aaff
<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"> speech:
<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"> object:
<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"> back </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 ("cannot send blank information ")
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 );
}
?>
<! -- Get the speech information 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 "

Echo "<script language =" JavaScript "> N ";
Echo "<! -- N ";
Echo "Var win = parent. Frames [0] n ";
Echo "Var doc=win.doc umentn ";
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 = "to <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" type = "codeph" text = "/codeph">
</Body>

</Html>
<! -- Welcome word 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 "

Echo "<script language =" JavaScript "> N ";
Echo "<! -- Hiden ";
Echo "Var win = parent. Frames [0] n ";
Echo "Var doc=win.doc umentn ";
Echo "Var divlin = Doc. Body. All (" Lin ") N ";
Echo "divlin. innerhtml =" <font color = blue> <B> welcome to $ username </B> </font> <br> "N ";
Echo "--> N ";
Echo "</SCRIPT> N ";
?>
<Body bgcolor = "#333333" type = "codeph" text = "/codeph">
</Body>
</Html>
<! --Display the statement 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>
Welcome </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.doc ument
VaR form1 = Doc. Body. All ("fayanform ")
Form1.objectname. value = Str
}
-->
</SCRIPT>
</Html>

<! -- List online users -->
<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>
Online users </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.doc ument
VaR form1 = Doc. Body. All ("fayanform ")
Form1.objectname. value = Str
}
-->
</SCRIPT>
</Body>
</Html>

[This article is copyrighted by the author and osuo. If you need to reprint it, please indicate the author and its source]

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.