Skip to content

Instantly share code, notes, and snippets.

@sotarok
Created December 8, 2008 07:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sotarok/33390 to your computer and use it in GitHub Desktop.
Save sotarok/33390 to your computer and use it in GitHub Desktop.
php simple chat
<?php
define("URL", "http://あなたの設置したPHPファイルのURL");
define("TIME", time());
define("__THIS_FILE__", file_get_contents(__FILE__));
define("__THIS_SCRIPT__", substr(__THIS_FILE__, 0, strpos(__THIS_FILE__, "__" . "halt_compiler();") + 19));
$logs = explode("\n", substr(__THIS_FILE__, strpos(__THIS_FILE__, "__" . "halt_compiler();") + 19));
if (strcasecmp($_SERVER['REQUEST_METHOD'], 'post') == 0) {
if (isset($_POST['text']) && !empty($_POST['text'])
&& isset($_POST['name']) && !empty($_POST['name'])) {
setcookie('name', $_POST['name']);
$logs_original = (count($logs) > 50) ? array_slice($logs, count($logs) - 50) : $logs;
if (!file_put_contents(__FILE__, __THIS_SCRIPT__ . join("\n", $logs_original)
. str_replace(array("\t", "\n", "\r"), "", $_POST['name']) . "\t" . str_replace(array("\t", "\n", "\r"), "", $_POST['text']) . "\t" . TIME . "\n", LOCK_EX)) {
echo "failed to write / please set permission 666 this file.<br>";
exit;
}
}
header("Location: " . URL);
}
else {
$name = isset($_COOKIE['name']) ? $_COOKIE['name'] : "";
?>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>php simple chat</title>
<style type="text/css">body {font-size: 18px;font-family: "メイリオ";background:#0f0f0f;color:#ccc;} a {color:#fff;} h2 {color: #fcc;} strong {color:#99c;}</style>
</head>
<body onload="document.getElementById('text-message').focus();">
<h1>php simple chat</h1>
<form method="post">
name : <input type="text" size="10" name="name" value="<?php echo htmlspecialchars($name, ENT_QUOTES); ?>"><br>
message : <input type="text" size="50" name="text" value="" id="text-message"> <input type="submit" value="Send / Reload"><br>
</form>
<h2>chat log</h2>
<div style="color: #ccc";>
<?php foreach (array_reverse($logs) as $m) {
if (!empty($m)) {
list($log_name, $log_message, $log_date) = explode("\t", $m);
echo '<strong>' . htmlspecialchars($log_name, ENT_QUOTES) . "</strong> said : " . htmlspecialchars($log_message, ENT_QUOTES) . " <em> -- " . date("Y-m-d H:i:s", $log_date) ." </em><br>\n";
}
}
?>
</div>
<div style="border-top: solid 5px #fff; margin-top: 10px; padding-top: 10px;">"<a href="http://gist.github.com/33390">php simple chat</a>" powered by <a href="http://d.hatena.ne.jp/sotarok/">sotarok</a> with <a href="http://nequal.jp">nequal</a></div>
</body>
</html>
<?php
}
exit;
__halt_compiler();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment