半个免数据库php聊天工具
半个免数据库php聊天工具,主要是把聊天信息存在文件里,文件路径是/dev/shm 这样能提高性能。
半个免数据库php聊天工具,主要是把聊天信息存在文件里,文件路径是/dev/shm 这样能提高性能。
<?php
date_default_timezone_set('UTC');
$contentArr = array();
$contentArr_path = '/dev/shm/chatarr.php';
if ($_POST) {
$newContent = $_POST['content'];
$file = fopen($contentArr_path , 'rw+');
flock($file , LOCK_EX | LOCK_NB);//独占锁,防止两个进程同时修改文件。
require $contentArr_path;
$contentArr[] = array('time' => date('H:i:s') , 'content' => $newContent);
$str = '<?php $contentArr = ' . var_export($contentArr , true) . ';';
fwrite($file , $str);
fclose($file);
}
else {
require $contentArr_path;
}
?>
<html>
<head></head>
<body>
<?php
foreach ($contentArr as $v) {
echo $v['time'] . ' ' . $v['content'] . '<br />';
}
?>
<form action="chat.php" method=post>
<input type="text" name="content" id='inputContent' value="" />
<input type="submit" />
</form>
<script>
var inputContent = document.getElementById('inputContent');
inputContent.focus();
function refresh() {
if ('' == inputContent.value) {
window.location.href="chat.php";
}
}
setTimeout('refresh()' , 2000);
</script>
</body>
</html>
精彩图集
精彩文章






