Created
February 9, 2015 18:15
-
-
Save iGlitch/d302f57b4ab60e80092b to your computer and use it in GitHub Desktop.
Basic PHP Chat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<tt> | |
<form action=chat.php method=get> | |
<?php | |
// ケータイっちゃ | |
define('MAX', 50); // ログ保存行数 | |
define('CLOG', 'chat.log'); // チャットログ | |
define('SLOG', 'sanka.log');// 参加者ログ | |
// 色リスト 上下対応 | |
$col[n] = array('黒','赤','青','緑','橙','紫','茶','灰','桃','白'); | |
$col[c] = array('black','red','blue','green','orange','purple','brown','gray','pink','#EEEEEE'); | |
// | |
$_GET = array_map('htmlspecialchars', $_GET); | |
if (get_magic_quotes_gpc()) { | |
$_GET = array_map('stripslashes', $_GET); | |
} | |
$l = ($_GET[l] == "") ? 10 : $_GET[l]; | |
$num = Sanka(up); // 参加者更新 | |
if ($_GET[ns] != "") { | |
Sanka(chk); // 名前重複 | |
$_GET[n] = $_GET[ns]; | |
} | |
if ($_GET[n] == "" || isset($_GET[t])) { | |
foreach ($col[n] as $k=>$v) { | |
$sel.="<option value=$k>$v"; | |
} | |
// 入室フォーム | |
echo <<<_INN | |
オープンチャット | |
<br><br> | |
現在: {$num}人<br> | |
名前<input name=ns type=text size=4><br> | |
文字<select name=c> | |
$sel | |
</select>色<br> | |
ログ<select name=l> | |
<option>5<option selected>10<option>15<option>30</select>行<br> | |
<input type=submit value="入室"> | |
<hr size=1> | |
_INN; | |
} | |
else { | |
$num = Sanka(reg); // 参加者登録 | |
$all = Sanka(all); // 参加者リスト | |
// 発言フォーム | |
echo <<<_COMM | |
現在: {$num}人<br> | |
<input type=hidden name=n value="$_GET[n]"> | |
<input type=hidden name=c value="$_GET[c]"> | |
<input name=m type=text size=16><br> | |
<input type=submit value="発言/更新"><br> | |
ログ<input name=l type=text size=2 value=$l>行 <a href="ch.php?">退室</a> | |
<!--<br>メンバー $all--> | |
<hr size=1> | |
_COMM; | |
} | |
// 発言 | |
if ($_GET[m] != '') { | |
Uplog($_GET[m]); | |
} | |
// ログ表示 | |
$fp = fopen(CLOG, "r"); | |
for ($i=0; $i<$l; $i++) { | |
echo fgets($fp,4096); | |
} | |
fclose($fp); | |
// 参加者 | |
function Sanka($act) { | |
$mem = file(SLOG); | |
$mem2 = array(); | |
switch($act) { | |
case(chk): | |
foreach($mem as $val) { | |
if (strstr($val, $_GET[ns])) { | |
die("<font color=red>その名前は既に使われています</font>"); | |
} | |
} | |
break; | |
case(reg): | |
array_unshift($mem, time()."\t".$_GET[n]."\t\n"); | |
$mem2 = $mem; | |
break; | |
case(up): | |
for ($i=0; $i<count($mem); $i++) { | |
list($ti, $na) = explode("\t", $mem[$i]); | |
if ($na == $_GET[n]) continue; | |
if ($ti + 150 > time()) array_push($mem2, $mem[$i]); | |
} | |
break; | |
case(all): | |
for ($i=0; $i<count($mem); $i++) { | |
$all = explode("\t", $mem[$i]); | |
$san.= $all[1]."#"; | |
} | |
return $san; | |
} | |
$fp = fopen(SLOG, "w"); | |
flock($fp, LOCK_EX); | |
fputs($fp, implode('', $mem2)); | |
fclose($fp); | |
return count($mem2); | |
} | |
// ログ更新 | |
function Uplog($msg) { | |
global $col; | |
$fc = $col[c][$_GET[c]]; | |
$now = gmdate("H:i", time()+9*3600); | |
$line = file(CLOG); | |
if (!strstr($line[0], "$_GET[n]><font color=$fc>$msg")) { | |
$fp = fopen(CLOG, "w"); | |
flock($fp, LOCK_EX); | |
fputs($fp, "$_GET[n]><font color=$fc>$msg</font>($now)<hr size=1>\n"); | |
for ($i=0; $i<MAX; $i++) { | |
fputs($fp, $line[$i]); | |
} | |
fclose($fp); | |
} | |
} | |
?> | |
<a href=/>戻る</a> <a href=http://glitchery.jo/>Glitch!</a> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment