对于支持PHP的免费个人主页空间不支持用户的MySQL,怎么编写留言板?其实留言板的编写还有一种很简单的方法,那就是操作文本文件的方式。我在嘻哈免费主页上做了一个,大家可以去留言哦!
写两个PHP文件(note.php和result.php)另外加一个保存留言记录的文本文件(note.txt)。
好了,先看例子:(note.php)
<html>
<head><title>小小留言本</title></head>
<style type="text/css">
<!--
.{ font-family: "宋体"; font-size: 9pt;color:#000000}
-->
</style>
<body bgcolor=#cccccc>
<center><font style=" color=red;font-size:12pt">小小留言本</font></center>
<form method="POST" action="result.php">
<center>
<table border=0 width=80%>
<tr>
<td width=20%>您的大名:
</td>
<td width=80%><input type=text name=nickname style=width:100%>
</td>
</tr>
<tr>
<td width=20%>电子邮件:
</td>
<td width=80%><input type=text name=email style=width:100%>
</td>
</tr>
<tr>
<td width=20%>留言:
</td>
<td width=80%><textarea name=note style=width:100%></textarea>
</td>
</tr>
<tr>
<td width=20%> </td>
<td width=80% align=center><input type="submit" value="确定"><input type="reset" value="重写">
</td>
</tr>
</table>
<hr>
<?
$f = fopen("note.txt","r");
$msg = fread($f,filesize("note.txt"));
fclose($f);
print "$msg";
?>
<p align=right><font style=" color=red;font-size:12pt">作者:<a href="mailto:[email protected]">东方一蛇</a><br>
主页:<a href="http://phpinto.126.com">PHP入门者园地</a></font>
</center>
</body>
</html>
接着,看result.php
<html>
<head>
<style type="text/css">
<!--
.{ font-family: "宋体"; font-size: 9pt;color:#000000}
-->
</style>
<body bgcolor=#cccccc>
<?
if ($nickname=="") {
print "<center><b><font color=#FF99FF>您的大名?</font><br>";
}
else if ($email=="") {
print "<center><b><font color=#FF99FF>您电子邮件?</font><br>";
}
else if ($note=="") {
print "<center><b><font color=#FF99FF>您没有要说的吗?</font><br>";
}else{
print "<p></p>";
$t = date(Y年m月d日);
$note = str_replace ( "<", "<", $note);
$note = str_replace ( ">", ">", $note);
$note = str_replace ( "\n", "<br>", $note);
$main = "网上大名:<a href=\"mailto:$email\">$nickname</a>:($t)<br>留言: $note <br><hr>";
$f = fopen("note.txt","a");
fwrite($f,$main);
fclose($f);
print "<center><b><font color=#FF99FF>谢谢您的留言!</font><br>";
}
?>
<p><br></p>
<p><br></p>
<center><a href="note.php">返回</a></center>
<p><br></p>
<p align=right><font style=" color=red;font-size:12pt">作者:<a href="mailto:[email protected]">东方一蛇</a><br>
主页:<a href="http://phpinto.126.com">PHP入门者园地</a></font>
</center>
</body>
</html>
最后,在同一目录下新建一个文本文件(note.txt),运行note.php就可以了!用读写文本文件的方法制作留言板,方法比较简单,涉及的相关知识比较少,大家应该一看就明白了,所以,在这里我不想多做解释。但是,在这里我要说明的是,在result.php中有三行用函数str_replace将留言进行一些处理,因为,如果不处理当用户输入“<script ...>”等之类的东西时可能会不能正常显示,而且可能不能处理用户输入的回车符号。还有,这三行中,最后一行不要放到前两行之前哦
|