龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

半个免数据库php聊天工具

时间:2014-07-22 14:49来源: 作者: 点击:
分享到:
半个免数据库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'] . '&nbsp;&nbsp;' . $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>
精彩图集

赞助商链接