PHP程序百行代码快速构建简易聊天室方法
PHP程序百行代码快速构建简易聊天室方法
基本功能:能登陆,聊天,记录在线人数IP事件,能控制聊天的字体颜色,自动把转化聊天中网址为链接地址,能定制聊天室标题、广告信息等等。使用文本作为存储媒体,又兴趣可以参考代码,并且进行扩展。其实PHP作为快速开发的脚本语言是很棒的!
===代码===
以下为引用的内容:
<?php
/**
* 黑夜路人简易聊天室
* 作者: heiyeluren <heiyeluren_at_163.com>
* 创建: 2005-8-10 22:42
* 修改: 2005-8-11 23:25
*/
error_reporting(7);
session_start();
header("ContentType:text/html;charset=gb2312");
define("SCRIPT", $_SERVER['SCRIPT_NAME']);
define("CHAT_NOTE", "./chat.txt");
define("ONLINE_LIST", "./online.txt");
define("REF_TIME", 5);
define("CHAT_NAME", "黑夜路人聊天室");
define("AD_MSG", "今天是中国情人节, 祝大家情人节快乐!!");
//获取值
if (isset($_GET['action']) && !empty($_GET['action'])) {
$action = $_GET['action'];
}
//如果已经登陆那么直接跳到聊天界面
if (!isset($_GET['action']) && isset($_SESSION['username'])) {
header("location:".SCRIPT."?action=chat");
}
//登陆提示
if (!isset($_GET['action']))
{
if (!session_is_registered('username'))
{
echo " <p><h3 align=center>[ ".CHAT_NAME." ] © 2005</h3></p>
<p align=center>
<form action=".SCRIPT."?action=login method=post>
呢称: <input type=text size=25 maxlength=30 name=login_user>
<input type=submit value=聊天>
</form></p>
";
exit;
}
}
//校验登陆
if ($action=='login')
{
if (isset($_POST['login_user']) && !empty($_POST['login_user'])) {
$username = $_POST['login_user'];
} else {
$username = "游客";
}
session_register('username');
save_online($username, get_client_ip());
header("location:".SCRIPT."?action=chat");
}
//开始聊天www.knowsky.com
if ($action=="chat")
{
$online_sum = get_online_sum();
echo "<head><title>[ ".CHAT_NAME." ]</title></head><center>
<body bgcolor=#C4BFB9 >
<div >
<iframe src='".SCRIPT."?action=show'
name=show_win width=800 height=450 scrolling=auto frameborder=0></iframe>
</div><br>
<marquee width=70% scrollamount=2> ".AD_MSG." </marquee>
[当前在线:$online_sum]
<iframe src='".SCRIPT."?action=say' name=say_win width=800
height=60 scrolling=no frameborder=0>
";
}
//说话界面
if ($action=="say")
{
echo "<head><title>[ ".CHAT_NAME." ]</title></head><center>
<body bgcolor=#C4BFB9 >
<form action=".SCRIPT."?action=save method=post name=chat
onSubmit='return check()'>
[".$_SESSION['username']."]说:<input type=text size=80
maxlength=500 name=chatmsg style=' background-color:#99CC99;
width:550px; height:22px; border:1px solid:#000000'>
<select name=usercolor>
<OPTION selected value='000000'>默认颜色</OPTION>
<OPTION value='#000000'>黑色沉静</OPTION>
<option value='#FF0000'>红色热情</option>
<option value='#0000FF'>蓝色开朗</option>
<option value='#FF00FF'>桃色浪漫</option>
<option value='#009900'>绿色青春</option>
<option value='#009999'>青色清爽</option>
<option value='#990099'>紫色拘谨</option>
<option value='#990000'>暗夜兴奋</option>
<option value='#000099'>深蓝忧郁</option>
<option value='#999900'>卡其制服</option>
<option value='#FF9900'>镏金岁月</option>
<option value='#0099FF'>湖波荡漾</option>
<option value='#9900FF'>发亮蓝紫</option>
<option value='#FF0099'>爱的暗示</option>