| 導購 | 订阅 | 在线投稿
分享
 
 
 

PHP程序百行代碼快速構建簡易聊天室方法

來源:互聯網  2008-06-20 06:03:01  評論

基本功能:能登陸,聊天,記錄在線人數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.knowsky.com

if ($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='".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='".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 style='COLOR: #000000' value='000000'>默認顔色</OPTION>

<OPTION style='COLOR: #000000' value='#000000'>黑色沈靜</OPTION>

<option style='COLOR: #ff0000' value='#FF0000'>紅色熱情</option>

<option style='COLOR: #0000ff' value='#0000FF'>藍色開朗</option>

<option style='COLOR: #ff00ff' value='#FF00FF'>桃色浪漫</option>

<option style='COLOR: #009900' value='#009900'>綠色青春</option>

<option style='COLOR: #009999' value='#009999'>青色清爽</option>

<option style='COLOR: #990099' value='#990099'>紫色拘謹</option>

<option style='COLOR: #990000' value='#990000'>暗夜興奮</option>

<option style='COLOR: #000099' value='#000099'>深藍憂郁</option>

<option style='COLOR: #999900' value='#999900'>卡其制服</option>

<option style='COLOR: #ff9900' value='#FF9900'>镏金歲月</option>

<option style='COLOR: #0099ff' value='#0099FF'>湖波蕩漾</option>

<option style='COLOR: #9900ff' value='#9900FF'>發亮藍紫</option>

<option style='COLOR: #ff0099' value='#FF0099'>愛的暗示</option>

<option style='COLOR: #006600' value='#006600'>墨綠深沈</option>

<option style='COLOR: #333333' value='#333333'>灰色軌迹</option>

<option style='COLOR: #999999' value='#999999'>倫敦灰霧</option>

</select>

<input type=submit value='說話' style='background-color:#ffffff'>

<a href=".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 style='font-size:12px' 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>\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;

}

?>

基本功能:能登陸,聊天,記錄在線人數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.knowsky.com if ($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='".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='".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 style='COLOR: #000000' value='000000'>默認顔色</OPTION> <OPTION style='COLOR: #000000' value='#000000'>黑色沈靜</OPTION> <option style='COLOR: #ff0000' value='#FF0000'>紅色熱情</option> <option style='COLOR: #0000ff' value='#0000FF'>藍色開朗</option> <option style='COLOR: #ff00ff' value='#FF00FF'>桃色浪漫</option> <option style='COLOR: #009900' value='#009900'>綠色青春</option> <option style='COLOR: #009999' value='#009999'>青色清爽</option> <option style='COLOR: #990099' value='#990099'>紫色拘謹</option> <option style='COLOR: #990000' value='#990000'>暗夜興奮</option> <option style='COLOR: #000099' value='#000099'>深藍憂郁</option> <option style='COLOR: #999900' value='#999900'>卡其制服</option> <option style='COLOR: #ff9900' value='#FF9900'>镏金歲月</option> <option style='COLOR: #0099ff' value='#0099FF'>湖波蕩漾</option> <option style='COLOR: #9900ff' value='#9900FF'>發亮藍紫</option> <option style='COLOR: #ff0099' value='#FF0099'>愛的暗示</option> <option style='COLOR: #006600' value='#006600'>墨綠深沈</option> <option style='COLOR: #333333' value='#333333'>灰色軌迹</option> <option style='COLOR: #999999' value='#999999'>倫敦灰霧</option> </select> <input type=submit value='說話' style='background-color:#ffffff'> <a href=".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 style='font-size:12px' 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>\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; } ?>
󰈣󰈤
王朝萬家燈火計劃
期待原創作者加盟
 
 
 
>>返回首頁<<
 
 
 
 
 熱帖排行
 
 
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有