網頁

2009/1/22

php講義 work9 陽春留言板

 這個留言板程式由四個檔案組成,這是一個分工寫程式的範例,不是成熟的實用品,請不要拿來用在嚴肅的地方,因為這支程式還有很多安全問題。
 四個檔案分別為資料檔mesg.dat,主程式index.php,將資料進mesg.dat的程式檔write.php,以及將資料從mesg.dat輸出到螢幕的程式show。另外,系統設定中,必須設定讓mesg.dat給所有人讀寫的能力。



表一 mesg.dat
01.
02.
03.
路人甲,1月22日9點32分,沒事多留言多留言沒事
王大德,1月22日9點32分,測試留言板
義工先生,1月22日9點33分,圖書館都是帥哥


表二 index.php
01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
11.
12.
13.
14.
<form method=post action=./index.php>
<input type=text name=user><br>
<textarea cols=100 rows=10 name=mesg></textarea><br>
<input type=submit>
</form>
<?php
include 'write.php';
include 'show';
$date=time();
$time=getdate($date);
$now="$time[mon]月$time[mday]日$time[hours]點$time[minutes]分";
$note = $_POST['user'].",".$now.",".$_POST['mesg']."\n";
writefile($note);
showmesg();


表三 write.php
01.
02.
03.
04.
05.
06.
<?
function writefile($mesg){
  $fp=fopen('mesg.dat','a');
  fwrite($fp,$mesg);
}
?>


表四 show
01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
11.
12.
13.
14.
<?
function showmesg(){
  $lines = file('mesg.dat');
  $count = count($lines);
  $i=0;
  while($i < $count){
    $element = explode(",",$lines[ $i]);
    echo "以下是".$element[0]."在".$element[1]."的留言:<br>";
    echo "<font size=20>".$element[2]."</font><br>";
    echo "<hr>";
    $i=$i+1;
  }
}
?>

沒有留言: