戻る      guest_input.php      guest_read2.php      guest.dat
     

《PHP言語 /guest_input.php》

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>ゲストブック</title>
</head>
<body>
<h1 style="background:#cccccc">ゲストブック(書き込み)</h1>
<form method="POST" action="guest_write_v2.php">
お名前:
<input type="text" name="name" size="20" maxlength="30" /><br />
メッセージ:
<input type="text" name="message" size="70" maxlength="255" /><br />
<input type="submit" value="送信" />
</form>
</body>
</html>



《PHP言語 /guest_read2.php》

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>ゲストブック</title>
</head>
<body>
<h1 style="background:#cccccc">ゲストブック(閲覧)</h1>

<?php
$file=fopen("guest.dat","r");
while($row=fgets($file,1024)){
$datum=explode("\t",$row);
?>
<ol>
<li>お名前:<?php print($datum[1]); ?></li>
<li>メッセージ:<?php print($datum[2]); ?></li>
<li>書き込み日時:<?php print($datum[0]); ?></li>
</ol>
<hr />
<?php
}
fclose($file);
?>

</body>
</html>

 




《PHP言語 /guest_write_v2.php》

<?php
$org_file=fopen("guest.dat","r");
$tmp_file=fopen("guest.tmp","w");
flock($org_file,LOCK_SH);
flock($tmp_file,LOCK_EX);
$line =date("Y年 m月 d日 H:i:s")."\t";
$line.=$_POST['name']."\t";
$line.=$_POST['message']."\t";
fputs($tmp_file,$line."\n");
while($row=fgets($org_file,1024)){
fputs($tmp_file,$row);
}
flock($tmp_file,LOCK_UN);
flock($org_file,LOCK_UN);
fclose($tmp_file);
fclose($org_file);
unlink("guest.dat");
rename("guest.tmp","guest.dat");
require_once("guest_input.php");header("Location: guest_input.php");変更

?>


《PHP言語 /guest.dat》