Do a QQ chat dating site, want to add a chat function, so finished using PHP to do a simple and powerful chat room
1. Creating a MySQL database table: CREATE TABLE Chat (id bigint auto_increment,username varchar (), chatdate datetime,msg, varchar (), prim ary key (ID));
2. Write the recommended connection database function:
dbconnect.php
<?php
function Db_connect ()
{
Date_default_timezone_set ("Asia/shanghai");
$link = mysql_connect ("xxx.xxx.xxx.xxx", "databasename", "password")
Or Die (' Unable to connect: '. Mysql_error ());
mysql_select_db ("DatabaseName") or Die (' not you find the specified database ');
return true;
}
function quote ($STRTEXT)
{
$Mstr = Addslashes ($strText);
Return "'". $Mstr. "'";
}
function IsDate ($d)
{
$ret = true;
Try
{
$x = Date ("D", $d);
}
catch (Exception $e)
{
$ret = false;
}
echo $x;
return $ret;
}
?>
3. Write Ajax send and receive functions:
Ajax Send function chat_send_ajax.php
<?php
Require_once (' dbconnect.php ');
Db_connect ();
$msg = Iconv ("UTF-8", "GB2312", $_get["MSG"]);
$DT = Date ("y-m-d h:i:s");
$user = Iconv ("UTF-8", "GB2312", $_get["name"]);
$sql = "INSERT into chat (username,chatdate,msg)".
"VALUES (". QUOTE ($user). "," . QUOTE ($DT). "," . QUOTE ($MSG). ");";
Echo $sql;
$result = mysql_query ($sql);
if (! $result)
{
throw new Exception (' Query failed: '. mysql_error ());
Exit ();
}
?>
Ajax receive Function chat_recv_ajax.php
<?php
Header ("content-type:text/html;charset=gb2312");
Header ("Expires:thu, 1970 00:00:01 GMT");
Header ("Cache-control:no-cache, must-revalidate");
Header ("Pragma:no-cache");
Require_once (' dbconnect.php ');
Db_connect ();
$sql = "SELECT *, Date_format (chatdate, '%y%m month%d%r ') as CDT from chat ORDER by ID desc limit 200";
$sql = "SELECT * from". $sql. ") as Ch order by ID";
$result = mysql_query ($sql) or Die (' Query failed: '. mysql_error ());
Update Row Information
$msg = "<table border= ' 0 ' style= ' font-size:10pt; Color:white; Font-family:verdana, Arial; > ";
while ($line = Mysql_fetch_array ($result, MYSQL_ASSOC))
{
$msg = $msg. "<tr><td>". $line [CDT]. "</td>".
"<td>". $line ["username"]. ": </td>".
"<td>". $line ["MSG"]. "</td></tr>";
}
$msg = $msg. "</table>";
Echo $msg;
?>
4. Chat Room page:
Chat.html
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> Chat Page </title>
<script type= "Text/javascript" >
var t = setinterval (function () {get_chat_msg ()},5000);
//
General Ajax Call
//
var oxmlhttp;
var oxmlhttpsend;
function get_chat_msg ()
{
if (typeof XMLHttpRequest!= "undefined")
{
Oxmlhttp = new XMLHttpRequest ();
}
else if (window. ActiveXObject)
{
Oxmlhttp = new ActiveXObject ("Microsoft.XMLHTTP");
}
if (oxmlhttp = null)
{
Alert ("Browser does not support XML Http request! ");
Return
}
Oxmlhttp.onreadystatechange = Get_chat_msg_result;
Oxmlhttp.open ("Get", encodeURI ("chat_recv_ajax.php"), true);
Oxmlhttp.send (NULL);
}
function Get_chat_msg_result ()
{
if (oxmlhttp.readystate==4 | | oxmlhttp.readystate== "complete")
{
if (document.getElementById ("div_chat")!= null)
{
document.getElementById ("Div_chat"). InnerHTML = Oxmlhttp.responsetext;
Oxmlhttp = null;
}
var scrolldiv = document.getElementById ("Div_chat");
Scrolldiv.scrolltop = Scrolldiv.scrollheight;
}
}
function set_chat_msg ()
{
if (typeof XMLHttpRequest!= "undefined")
{
Oxmlhttpsend = new XMLHttpRequest ();
}
else if (window. ActiveXObject)
{
Oxmlhttpsend = new ActiveXObject ("Microsoft.XMLHTTP");
}
if (oxmlhttpsend = null)
{
Alert ("Browser does not support XML Http request! ");
Return
}
var url = "chat_send_ajax.php";
var strname= "Noname";
var strmsg= "";
if (document.getElementById ("txtname")!= null)
{
strname = document.getElementById ("Txtname"). Value;
document.getElementById ("Txtname"). Readonly=true;
}
if (document.getElementById ("txtmsg")!= null)
{
strmsg = document.getElementById ("txtmsg"). Value;
document.getElementById ("Txtmsg"). Value = "";
}
url = "? name=" + strname + "&msg=" + strmsg;
Oxmlhttpsend.open ("Get", encodeURI (URL), true);
Oxmlhttpsend.send (NULL);
}
function Clickbtn (e)
{
if (window.event.keycode==13)
{
var id=e.id;
Switch (ID)
{
Case "Txtmsg":
document.getElementById ("Submit2"). Click ();
Window.event.returnvalue=false;
Break
}
}
}
function Frandomby (under, over) {
Switch (arguments.length) {
Case 1:return parseint (Math.random () *under+1);
Case 2:return parseint (Math.random () * (over-under+1) + under);
Default:return 0;
}
}
function Settxtname () {
var I=frandomby (10);
if (i==0) document.getElementById (' txtname '). Value= ' Invincible Ares ';
if (i==1) document.getElementById (' txtname '). value= ' Make Fox Rush ';
if (i==2) document.getElementById (' txtname '). value= ' Simon blows Snow ';
if (i==3) document.getElementById (' txtname '). value= ' Super Marie ';
if (i==4) document.getElementById (' txtname '). value= ' Obama ';
if (i==5) document.getElementById (' txtname '). value= ' terrorists ';
if (i==6) document.getElementById (' txtname '). Value= ' A strange woman in a lonely studio ';
if (i==7) document.getElementById (' txtname '). value= ' Celestial pen?
if (i==8) document.getElementById (' txtname '). value= ' 5 million;
if (i==9) document.getElementById (' txtname '). Value= ' Divine miracle ';
if (i==10) document.getElementById (' txtname '). value= ' Love you not two or three days ';
}
</script>
<body onload= "Settxtname ();" >
<div style= "Border-right:black thin solid; Border-top:black thin solid;
Border-left:black thin solid; Border-bottom:black thin solid;
Background: #fff url (' http://www.ihaonet.com/chat/blue.jpg ') repeat-x left top;
height:450px;width:500px; ">
<table style= "width:100%; height:100% ">
<tr>
<TD colspan= "2" style= "font-weight:bold; font-size:16pt; Color:white; Font-family:verdana, Arial;
Text-align:center ">
Chat Window--the world's largest QQ chat dating site </td>
</tr>
<tr>
<TD colspan= "2" style= "font-weight:bold; font-size:16pt; Color:white; Font-family:verdana, Arial;
Text-align:left ">
<table style= "FONT-SIZE:12PT; Color:white; Font-family:verdana, arial;border:white thin solid; ">
<tr>
<TD style= "width:100px" >
Name:</td>
<TD style= "width:100px" ><input id= "txtname" style= "width:150px" type= "text" name= "name" maxlength= "value" = "Anonymous"/></td>
</tr>
</table>
</td>
</tr>
<tr>
<TD style= "Vertical-align:middle" valign= "Middle" colspan= "2" >
<div style= "width:480px; height:300px; Border-right:white thin solid; Border-top:white thin solid; font-size:10pt; Border-left:white thin solid; Border-bottom:white thin solid; Font-family:verdana, Arial; Overflow:scroll; Text-align:left "id=" Div_chat ">
</div>
</td>
</tr>
<tr>
<TD style= "width:310px" >
<input id= "txtmsg" style= "width:350px" type= "text" Name= "MSG" onkeydown= "return clickbtn (This)"/></td>
<TD style= "width:85px" >
<input id= "Submit2" style= Font-family:verdana, Arial "type=" button "value=" Send "onclick=" set_chat_msg () "/><" /TD>
</tr>
<tr>
<TD colspan= "1" style= "Font-family:verdana, Arial; Text-align:center; width:350px; " >
</td>
<TD colspan= "1" style= "width:85px; Font-family:verdana, Arial; Text-align:center ">
</td>
</tr>
</table>
</div>
</body>