您的位置:首页技术文章
文章详情页

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

【字号: 日期:2023-12-27 08:26:42浏览:4作者:猪猪

无聊情况下,看能用比较少的代码构建聊天室,一开始写了整整100行把聊天室内完成,后来觉得很多功能没有,又重新修改,加了不少代码。其实不利用特别复杂的技术,比如模板、数据库等等,一样能做比较不错的聊天室,适合个人使用。

基本功能:能登陆,聊天,记录在线人数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.' ] &copy; 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.mypchelp.cnif ($action=='chat'){$online_sum = get_online_sum();echo '<head><title>[ '.CHAT_NAME.' ]</title></head><center><body bgcolor=#C4BFB9 style='font-size:12px;'> <div style='border:1px solid #999966; width:802px;height:450'><iframe src='https://www.haobala.com/bcjs/21083.html'.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>&nbsp;&nbsp; [当前在线:$online_sum] <iframe src='https://www.haobala.com/bcjs/21083.html'.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 style='font-size:12px;'> <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> <option value='#006600'>墨绿深沉</option> <option value='#333333'>灰色轨迹</option> <option value='#999999'>伦敦灰雾</option> </select> <input type=submit value='说话' style='background-color:#ffffff'> <a href='https://www.haobala.com/bcjs/.SCRIPT.'?action=logoff title=退出聊天室 target=_top onclick='return confirm('你确定要退出聊天室吗?')'>退出</a> </form> <script>function check(){if(document.chat.chatmsg.value==''){;alert('请输入聊天信息!');return false;}return true;}</script> ';}

//保存说话if ($action=='save'){if ($_POST['chatmsg']!='') { save_chat($_POST['chatmsg'], $_SESSION['username'], $_POST['usercolor']);}header('location:'.SCRIPT.'?action=say');}

//显示聊天记录if ($action=='show'){echo '<body onload='scrollit()'>';echo '<META HTTP-EQUIV=REFRESH CONTENT=''.REF_TIME.';URL='.SCRIPT.'?action=show'>';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_)*/', '<a href=0 target=_blank>0</a>', $msg);$msg = preg_replace('/([a-zA-Z0-9_.])+@([a-zA-Z0-9-])+.([a-zA-Z0-9-]{2,4})+/', '<a href=mailto:0>0</a>', $msg);$msg = date('H:i:s').' ['.$user.']说: <font color=''.$color.''>'.$msg.'</font><br>rn';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().'rn';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;}}//获取当前登陆用户IPfunction 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;}?>

标签: PHP