新增:用户注册,用户发言表情和字体颜色,在线用户统计,注销用户。
主要有4个界面,代码如下:
登陆界面:
<!--login.php-->
<html>
<head>
<title>uesr login</title>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<script type="text/javascript">
function nameGetFocus(){
// alert("please input your password");
document.frmLogin.user_name.focus();
}
function CheckValid(){
// alert("please input your password");
// var testt=documet.frmLogin.user_name.value;
// alert(testt);
if(document.getElementById("lee").value==""){
alert("please input your nickname!!!");
document.frmLogin.user_name.focus();
return false;
}
if (document.frmLogin.password.value=="") {
alert("please input your password");
document.frmLogin.password.focus();
return false;
}
return true;
}
</script>
</head>
<body οnlοad="nameGetFocus()" bgcolor="#ffffff" text="#000000">
<h1 align="center"> welcome to my chatting room</h1>
<table width="75%" border="0" align="center" bgcolor="#ffffff">
<tr>
<td align="center"> <img src="imges/logo.gif" width="250" height="70">
</td>
</tr>
<tr>
<td align="center">
<form name="frmLogin" method="post" action="check_user.php">
NickName:
<input type="text" id="lee" name="user_name"/>
Code :
<input type="password" name="password"/>
<input type="submit" name="cmdLogin" value="Login" οnclick="return CheckValid();"/>
</form>
</td>
</tr>
<tr>
<td align="center"><font color="#000099">If this is your first time to login my chatting room,
system will register your nickname.</font>
</td>
</tr>
</table>
</body>
</html>
用户检查界面:
<?php
session_start();
$user_name1=$_POST["user_name"];
// echo $user_name1;
$_SESSION["user_name"]=$user_name1;
$pwd=$_POST["password"];
// echo "$user_name1";
// echo "$pwd";
require_once("sys_conf.inc");
$link_id=mysql_connect($DBHOST,$DBUSER,$DBPWD);
// require("main.php");
// mysql_select_db($DBNAME);
if(mysql_select_db("newchat")){
// echo "database selecede";
}
else{
echo "not select data base ";
}
$str="select name , password from user where name='$user_name1'";
$result=mysql_query($str,$link_id);
@$rows=mysql_num_rows($result);
if($rows!=0){
list($name,$password)=mysql_fetch_row($result);
if ($pwd==$password) {
$str="update user set is_online=1 where name='$user_name1'
and password='$pwd'";
echo mysql_error();
$result=mysql_query($str,$link_id);
require("main.php"); # code...
}
else{
require("reload.php");
}
}else{
$str="insert into user (name,password,is_online) values('$user_name1','$pwd',1)";
$result=mysql_query($str,$link_id);
require("main.php");
}
mysql_close($link_id);
?><span style="white-space:pre"> </span>
主界面
<pre name="code" class="php"><!--chat.php-->
<html>
<title>chatting room</title>
<frameset cols="*,20%" cols="80%,*">
<frameset rows="80%,*">
<frame src="chat_display.php" name="chat_display">
<frame src="speak.php" name="speak">
</frameset>
<frameset rows="80%,*">
<frame name="top" src="online_number.php">
<frame name="exit" src="exit.php">
</framset>
</framset>
</html>
密码输入错误中心加载界面:
<pre name="code" class="php"><!--login.php-->
<html>
<head>
<title>uesr login</title>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<script type="text/javascript">
function nameGetFocus(){
// alert("please input your password");
document.frmLogin.user_name.focus();
}
function CheckValid(){
// alert("please input your password");
// var testt=documet.frmLogin.user_name.value;
// alert(testt);
if(document.getElementById("lee").value==""){
alert("please input your nickname!!!");
document.frmLogin.user_name.focus();
return false;
}
if (document.frmLogin.password.value=="") {
alert("please input your password");
document.frmLogin.password.focus();
return false;
}
return true;
}
</script>
</head>
<body οnlοad="nameGetFocus()" bgcolor="#ffffff" text="#000000">
<h1 align="center"> Your password is not right,please input agine!!!!!</h1>
<h1 align="center"> welcome to my chatting room</h1>
<table width="75%" border="0" align="center" bgcolor="#ffffff">
<tr>
<td align="center"> <img src="imges/logo.gif" width="250" height="70">
</td>
</tr>
<tr>
<td align="center">
<form name="frmLogin" method="post" action="check_user.php">
NickName:
<input type="text" id="lee" name="user_name"/>
Code :
<input type="password" name="password"/>
<input type="submit" name="cmdLogin" value="Login" οnclick="return CheckValid();"/>
</form>
</td>
</tr>
<tr>
<td align="center"><font color="#000099">If this is your first time to login my chatting room,
system will register your nickname.</font>
</td>
</tr>
</table>
</body>
</html>
显示聊天内容界面:
<pre name="code" class="php"><html>
<head>
<title>show user leave message</title>
<meta http-equiv="refresh" content="8;url=chat_display.php">
</head>
<body>
<?php
require_once("sys_conf.inc");
$link_id=mysql_connect($DBHOST,$DBUSER,$DBPWD) or die("connect failed" . mysql_error());
if(mysql_select_db($DBNAME)){
// echo "database selecede";
}
else{
echo "not select data base ";
}
$str="select * from chattable order by create_time;";
$result=mysql_query($str,$link_id);
if($result)
$rows=mysql_num_rows($result);
else
{
echo "no selcet!!".$result." reslut" . mysql_error();
}
@mysql_data_seek($result, $rows-15);
if ($rows<15) {
# code...
$l=$rows;
}else{$l=15;
}
for($i=1;$i<=$l;$i++){
list($cid,$author,$create_time,$text,$color,$emotion)=mysql_fetch_row($result);
echo "<table>";
if($text!=""){
echo "<tr>";
echo "<td align=left><font color=$color>$create_time";
echo "[ ".$author." ]".$emotion." say: $text</font><td>";
echo "</tr>";
}
echo "</table>";
}
@mysql_data_seek($result,$rows-20);
list($limtime)=mysql_fetch_row($result);
$str="delete from chattable where create_time<'$limtime';";
mysql_query($str,$link_id);
mysql_close($link_id);
?>
</body>
</html>
在线用户检测:
<pre name="code" class="php"><!--online_number.php-->
<html>
</head>
<title>online number</title>
<meta http-equiv="refresh" content="5;url=online_number.php">
</head>
<body>
<h4>online friends</h4>
<?php
require_once("sys_conf.inc");
$link_id=mysql_connect($DBHOST,$DBUSER,$DBPWD) or die("connect failed" . mysql_error());
if(mysql_select_db("newchat")){
// echo "database selecede";
}
else{
echo "not select data base ";
}
$str="select * from user where is_online!=0 order by uer_id ";
echo mysql_error();
$result=mysql_query($str,$link_id);
// if($result)
echo "<table>";
while ($row=mysql_fetch_row($result)) {
# code...
// echo "[$row]";
$name=$row[1];
echo "<tr>";
echo "<td>[$name]</td>";
echo "<tr>";
}
echo "</table>";
mysql_close($link_id);
?>
</body>
</html>
发言界面:
<pre name="code" class="php"><?php session_start();?>
<html>
<head>
<title>speak</title>
</head>
<body>
<?php
require_once("sys_conf.inc");
if (isset($_POST["slt_text_color"])) {
switch ($_POST["slt_text_color"]) {
case "red":
# code...
$color1="red";
break;
case "blue":
$color1="blue";
break;
case 'gray':
$color1="gray";
default:
# code...
$color1="black";
break;
}
# code...
}
if ($_POST["text"] &&$_POST["text"]!=""){
$link_id=mysql_connect($DBHOST,$DBUSER,$DBPWD);
mysql_select_db($DBNAME);
$time=date("h;i;s");
$text1=$_POST["text"];
$author=$_SESSION["user_name"];
// echo $author;
$emotion1=$_POST["emotion"];
// $author="lee";
// $text=$_POST["text"];
$str="insert into chattable
(create_time,author,text,color,emotion)
values('$time','$author','$text1','$color1','$emotion1');";
mysql_query($str,$link_id);
echo mysql_error();
mysql_close();
}
# code...
?>
<form action="speak.php" method="post" target="_self">
<input type="text" name="text" cols="20">
<input type="submit" value="send">
<select size=1 name="slt_text_color">
<?php
switch ($color) {
case 'red':
$slt_text_color="red";
# code...
break;
case 'blue':
$slt_text_color="blue";
break;
case 'gray':
# code...
$slt_text_color="gray";
break;
default:
# code...
$slt_text_color="";
break;
}
if (isset($slt_text_color)) {
?>
<option selected><?php
echo $slt_text_color;}
?>
</option>
<option>red</option>
<option>blue</option>
<option>gray</option>
</select>
<select size=1 name="emotion">
<?php
if (isset($emotion)) {
# code...
?>
<option selected><?php echo $emotion;}?></option>
<option>shaine</option>
<option>happy</option>
<option>sad</option>
</select>
</form>
</body>
</html>
退出:
<pre name="code" class="php"><?php
session_start();
?>
<html>
<head>
<title>leave chatting room</title>
<script type="text/javascript">
function exit(){
// window.top.location ="login.php";
window.top.location.replace("login.php") ;
return true;
}
</script>
</head>
<body>
<form name="frmExit" method="post" action="exit.php">
UserName: <?php echo $_SESSION["user_name"]?>
<br>
<input type="submit" name="cmdExit" value="leaveSys" οnclick="exit()">
</form>
<?php
require_once("sys_conf.inc");
$link_id=mysql_connect($DBHOST,$DBUSER,$DBPWD);
// mysql_select_db($DBNAME);
if(mysql_select_db("newchat")){
// echo "database selecede";
}
else{
echo "not select data base ";
}
$str="update user set is_online=0 where name='$_SESSION[user_name]'";
// echo "hahaha";
$result=mysql_query($str,$link_id);
mysql_close($link_id);
?>
</body>
</html>
数据库配置:
<!--system configur-->
<?php
$DBHOST='localhost';
$DBUSER='root';
$DBPWD='';
$DBNAME='newchat';
$DBTABLEUSER="user";
$DBTABLECHATTABLE="chattable";
?>