下面我用讀寫文本文件的方式給大家簡單介紹一下聊天室的制作。該聊天室一共有四個主要的PHP文件:
login.php用來登錄
<html>
<body>
<form action="chat.php">
房 間:<select name="room" >
<option value="大廳">大廳</option>
<option value="客房">客房</option>
<option value="後院">後院</option>
</select>
您的大名:<input type="text" name="name">
<input type=submit value="進入">
</form>
</body>
</html>
chat.php為主文件
<html>
<head>
<title>簡易聊天室(作者:東方一蛇(http://phpinto.126.com))</title>
</head>
<frameset rows="80%,*" cols="*">
<frame src="view.php?room=<?php echo $room; ?>">
<frame src="input.php?name=<?php echo $name; ?>&room=<?php echo $room; ?>">
</frameset>
<noframes>
<body bgcolor="#cccccc">
</body></noframes>
</html>
view.php用來顯示聊天
<html>
<meta http-equiv="Refresh" content="5; url=view.php?room=<?php echo $room; ?>">
<body bgcolor="#cccccc">
<?
switch ($room) {
case '大廳':
$write_file="1.txt";
break;
case '客房':
$write_file="2.txt";
break;
case '後院':
$write_file="3.txt";
break;
default:
$write_file="0.txt";
break;
}
$chat_lenght = 25;
$lines = file($write_file);
$a = count($lines);
$u = $a - $chat_lenght;
for($i = $a; $i >= $u ;$i--){
echo $lines[$i] . "<br>";
}
?>
</body>
</html>