学习网考试学习资料

Gzu521.com

PHP百行代码快速构建简易聊天室

PHP教程   点击:次   发布时间:2005-8-19   【字体: 】   来源:Gzu521.com
Gzu521.com我的学习网
无聊情况下,看能用比较少的代码构建聊天室,一开始写了整整100行把聊天室内完成,后来觉得很多功能没有,又重新修改,加了不少代码。其实不利用特别复杂的技术,比如模板、数据库等等,一样能做比较不错的聊天室,适合个人使用。

基本功能:能登陆,聊天,记录在线人数ip事件,能控制聊天的字体颜色,自动把转化聊天中网址为链接地址,能定制聊天室标题、广告信息等等。使用文本作为存储媒体,又兴趣可以参考代码,并且进行扩展。

其实php作为快速开发的脚本语言是很棒的!

===代码===

/**
 * 黑夜路人简易聊天室
 * 作者: heiyeluren 
 * 创建: 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 " 

[ ".chat_name." ] ©; 2005


   
   
   呢称: 
   
    `0+s?A@Loj([-l [ 本 资 料 来 源 于 贵 州 学 习 网 网络编程PHP教程 http://Www.gzU521.com ] `0+s?A@Loj([-l


   ";
  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 "[ ".chat_name." ]

   

    ".ad_msg."  ; ; [当前在线:$online_sum]
   
 ";
}

//说话界面
if ($action=="say")
{
 echo "[ ".chat_name." ]

  
  [".$_session[’username’]."]说:
  
  默认颜色
  黑色沉静 
  红色热情 
  蓝色开朗 
  桃色浪漫 
  绿色青春 
  青色清爽 
  紫色拘谨 
  暗夜兴奋 
  深蓝忧郁 
  卡其制服 
  镏金岁月 
  湖波荡漾 
  发亮蓝紫 
  爱的暗示 
  墨绿深沉 
  灰色轨迹 
  伦敦灰雾 
  
   &#111nclick=’return confirm(\"你确定要退出聊天室吗?\")’>退出
  
  function check(){if(document.chat.chatmsg.value==’’){;alert(’请输入聊天信息!’);return false;}return true;}
  ";
}

//保存说话
if ($action=="save")
{
 if ($_post[’chatmsg’]!="") {
  save_chat($_post[’chatmsg’], $_session[’username’], $_post[’usercolor’]);
 }
 header("location:".script."?action=say");
}

//显示聊天记录
if ($action=="show")
{
 echo "";
 echo "";
 if (file_exists(chat_note)) {
  $chat_msg = @file_get_contents(chat_note);
  echo $chat_msg;
 } else {
  echo "目前没有人说话";
 }
}

//退出聊天室
if ($action=="logoff")
{
 unset($_session[’username’]);
 session_destroy();
 header("location:".script);
}

/* 基本函数 */

//保存聊天记录函数
function save_chat($msg, $user, $color)
{
 if (!$fp = fopen(chat_note, "a+")) {
  die(’创建聊天记录文件失败, 请检查是否有权限.’);
 }
 $msg = htmlspecialchars($msg);
 $msg = preg_replace(’/([http|Ftp:\/\/])*([a-za-])+\.([a-za-z0-9_-])+\.([a-za-z0-9_-])+(a-za-z0-9_)*/’, ’\\0’, $msg);
 $msg = preg_replace(’/([a-za-z0-9_\.])+@([a-za-z0-9-])+\.([a-za-z0-9-]{2,4})+/’, ’\\0’, $msg);
 $msg = date(’h:i:s’)." [".$user."]说: ".$msg."
\r\n";
 if (!fwrite($fp, $msg)) {
  die(’写入聊天记录失败.’);
 }
 fclose($fp);
}
//写在线人信息
function save_online($user, $ip)
{
 if (!$fp = fopen(online_list, "a+")) {
  die("创建在线列表文件失败, 请检查是否有权限.");
 }
 $user = str_replace("|", "", $user);
 $line = $user."|".$ip."|".time()."\r\n";
 if (!fwrite($fp, $line)) {
  die("写入在线列表失败.");
 }
 fclose($fp);
}
//获取在线人数
function get_online_sum()
{
 if (file_exists(online_list)) {
  $online_msg = file(online_list);
  return count($online_msg);
 } else {
  return 0;
 }
}
//获取当前登陆用户ip
function get_client_ip()
{
 if ($_server[’remote_addr’]) {
  $cip = $_server[’remote_addr’];
 } elseif (getenv("remote_addr")) {
  $cip = getenv("remote_addr");
 } elseif (getenv("http_client_ip")) {
  $cip = getenv("http_client_ip");
 } else {
  $cip = "unknown";
 }
 return $cip;
}
?>

责任编辑:gzu521

网络编程分类
ASP教程
.Net教程
Java教程
PHP教程
数据库基础
ACCESS教程
SQL Server教程
MySQL教程
Oracle教程
分类推荐信息
更多...
大类最新文章
更多...