作者:晴子suerw_980 | 来源:互联网 | 2018-03-22 13:42
?php*ajaxphp聊天室实例代码但是必须基于以下条款:署名。你必须明确标明作者的名字。.非商业用途。你不...
"欢迎来到迷你ajax聊天室。最新版本 1.2。下载请到www.phprm.com",
//聊天室标题
"title" => "mini ajax chatroom by longbill",
//第一个到聊天室的欢迎
"firstone" => "welcome to longbill's mini ajax chatroom!",
//当信息有禁止内容时显示
"ban" => "i am a pig!",
//关键字
"keywords" => "聊天室,迷你,小型,ajax,chat,chatroom,longbill,phprm.com,php,网页特效",
//发言提示
"hereyourwords" => "在这里发言!"
);
error_reporting(e_all ^ e_notice ^ e_warning);
header("content-type:text/html; charset=utf-8");
$get_past_sec = 3; //如果发现丢话,可以适当调大这个值
$touchs = 10; //检查在线人数的时间间隔
if (!function_exists("file_get_contents")) {
function file_get_contents($path) {
if (!file_exists($path)) return false;
$fp = @fopen($path, "r");
$all = fread($fp, filesize($path));
fclose($fp);
return $all;
}
}
if (!function_exists("file_put_contents")) {
function file_put_contents($path, $val) {
$fp = @fopen($path, "w");
fputs($fp, $val);
fclose($fp);
return true;
}
}
$title = $lang["title"];
$earlier = 10;
$description = $lang["description"];
$origroom = $room;
$least = ($_get["dis"]) ? intval($_get["dis"]) : $leastnum;
$touchme = $_post['touchme'];
if (!is_dir($roomdir)) @mkdir($roomdir) or die("error when creating folder $roomdir");
$room = $_get['room'];
if (!$room) $room = $_post["room"];
$room = checkfilename($room);
if (!$room) $room = $origroom;
$filename = $roomdir . $room . ".dat.php";
$datafile = $roomdir . $room . ".php";
if (!file_exists($filename)) @file_put_contents($filename, '' . " " . time() . "|" . $lang["firstone"] . " ");
if (!file_exists($datafile)) @file_put_contents($datafile, '' . " ");
$action = $_post["action"];
function checkfilename($file) {
if (!$file) return "";
$file = trim($file);
$a = substr($file, -1);
$file = eregi_replace("^[./]*", "", $file);
$file = eregi_replace("[./]*$", "", $file);
$arr = array(
"../",
"./",
"/",
"",
"..",
"."
);
$file = str_replace($arr, "", $file);
return $file;
}
function get_ip() {
global $_server;
if ($_server) {
if ($_server[http_x_forwarded_for]) $realip = $_server["http_x_forwarded_for"];
else if ($_server["http_client_ip"]) $realip = $_server["http_client_ip"];
else $realip = $_server["remote_addr"];
} else {
if (getenv('http_x_forwarded_for')) $realip = getenv('http_x_forwarded_for');
else if (getenv('http_client_ip')) $realip = getenv('http_client_ip');
else $realip = getenv('remote_addr');
}
return $realip;
}
function array2json($arr) {
$keys = array_keys($arr);
$isarr = true;
$json = "";
for ($i = 0; $i 20) die('error');
for ($i = 0; $i ", $content);
$cOntent= ($style) ? "{$content}" : $content;
$s.= $t . "|" . $name . ":" . $content . " ";
}
if (!$s) die("no content!!");
$fp = @fopen($filename, "a+");
if (!$fp) die("repeat");
$re_time = 0;
while (!@flock($fp, lock_ex)) {
sleep(1);
$re_time++;
if ($re_time >= 4) break;
}
if ($re_time <4) {
@fputs($fp, $s);
@flock($fp, lock_un);
} else die("repeat");
@fclose($fp);
echo "ok";
} else if ($action == "read") {
$first = $_post["first"];
$lastmod = intval($_post["lastmod"]) - $get_past_sec; //得到两秒以内的所有发言,
$alastmod = @filemtime($filename);
if ($lastmod - $alastmod > 360 * 48) die;
$name = $_post[&#39;name&#39;];
$name = str_replace(" ", "", $name);
$ip = get_ip();
$json = array();
$json["lastmod"] = time();
$item = array();
$newOnline= array();
$offline = array();
$fp = @fopen($filename, &#39;r&#39;);
flock($fp, lock_ex);
$s = fread($fp, filesize($filename));
flock($fp, lock_un);
fclose($fp);
$lines = explode(" ", $s);
if ($alastmod >= $lastmod && !$first) {
foreach ($lines as $l) {
$item2 = array();
$l = str_replace(array(
" ",
" "
) , "", $l);
if (strpos($l, "|") === false) continue;
$arr = explode("|", $l);
$t = intval($arr[0]);
if ($t >= $lastmod) {
$item2["time"] = date("h:i:s", $t);
$item2["word"] = addslashes($arr[1]);
$item[] = $item2;
}
}
} else if ($first) {
$item = array();
$total = count($lines);
for ($i = $total - 1; $i >= $total - $least; $i--) {
if ($i <= 0) break;
$item2 = array();
$l = str_replace(array(
" ",
" "
) , "", $lines[$i]);
if (strpos($l, "|") === false) continue;
$arr = explode("|", $l);
$t = intval($arr[0]);
$item2["time"] = (date("m-d", time()) == date("m-d", $t)) ? date("h:i:s", $t) : date("m-d h:i", $t);
$item2["word"] = addslashes($arr[1]);
$item[] = $item2;
}
$item = array_reverse($item);
}
$s = "";
$nt = time();
$Onlines= array();
if ($disonline && $touchme) {
$users = @file($datafile);
foreach ($users as $l) {
$l = str_replace(array(
" ",
" "
) , "", $l);
if (strpos($l, "|") === false) {
$s.= $l . " ";
continue;
}
$arr = explode("|", $l);
if ($nt - intval($arr[1]) <$touchs * 3) {
if (trim($name) == trim($arr[2])) {
$s.= $arr[0] . "|" . time() . "|" . $name . "|" . get_ip() . "| ";
} else $s.= $l . " ";
$onlines[] = htmlspecialchars($arr[2]);
}
}
@file_put_contents($datafile, $s);
$json["onlines"] = $onlines;
}
$json["lines"] = $item;
echo array2json($json);
} else if ($action == "keep") {
keeponline();
echo "keep ok";
} else if ($action == "quit") {
$name = $_post[&#39;name&#39;];
if ($disonline) {
$users = @file($datafile);
foreach ($users as $l) {
$l = str_replace(array(
" ",
" "
) , "", $l);
if (strpos($l, "|") === false) {
$s.= $l . " ";
continue;
}
$arr = explode("|", $l);
if (trim($name) == trim($arr[2])) continue;
else $s.= $l . " ";
}
@file_put_contents($datafile, $s);
echo "ok";
}
die();
} else {
?>
" />
">
">
loading...
name:
size:(px)
font:
bold:
window:+
-
clear
&#39; id=&#39;p_online&#39;>loading online...