<?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
呢称:
“; 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.cncms.comif ($action==”chat”){ $online_sum = get_online_sum(); echo “ [ “.CHAT_NAME.” ]<body bgcolor=#C4BFB9 style=’font-size:12px;’>
<iframe src='”.SCRIPT.”?action=show’name=show_win width=800 height=450 scrolling=auto frameborder=0></iframe>
<marquee width=70% scrollamount=2> “.AD_MSG.” </marquee> [当前在线:$online_sum] <iframe src='”.SCRIPT.”?action=say’ name=say_win width=800height=60 scrolling=no frameborder=0> “;}//说话界面if ($action==”say”){ echo “ [ “.CHAT_NAME.” ]<body bgcolor=#C4BFB9 style=’font-size:12px;’> [“.$_SESSION[‘username’].”]说: 默认颜色 黑色沉静 红色热情 蓝色开朗 桃色浪漫 绿色青春 青色清爽 紫色拘谨 暗夜兴奋 深蓝忧郁 卡其制服 镏金岁月 湖波荡漾 发亮蓝紫 爱的暗示 墨绿深沉 灰色轨迹 伦敦灰雾 退出 <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 style=’font-size:12px’ >”; echo “<meta HTTP-
本&文来源gaodai^.ma#com搞#代!码网
搞gaodaima代码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_)*/’, ‘\\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; }}//获取当前登陆用户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;}?> |