当前位置:首页 > 服务器类 > 安全防护 > > 最简单的留言板(PHP)

最简单的留言板(PHP)

点击次数:16 次 发布日期:2008-11-29 17:40:15 作者:源代码网
源代码网推荐

1.1 留言板的原理

先让我们来看看现实生活中留言板是如何工作的,想象这样一种情况:
你说好在家等你的女友,可是临时有事情要出门,于是你决定写一个留言,以免回来时被修理。
让我们仔细分析一下事情的经过:

1 构思留言,想一个巧妙的理由
2 写下留言
3 将留言贴到门上,这样你的女友才能看到

其实在网络中,留言板也是同样工作的:

1 在页面上写好留言
2 将留言保存下来
3 在适当的时候适当的地方向适当的人显示留言

下边是这个最简单的留言板工作方式的示意图:



1.2 留言板的实现

下面让我们来考虑这几个页面的实现。
POST.PHP很简单,它只需要一个From,就OK了。





<-- /// POST.PHP: This web will submit the data to SAVE.php ///
///////////////////////////////////////////////////////////
-->


源代码网整理以下
题目:


源代码网整理以下内容:


源代码网整理以下


源代码网整理以下




如果你看不懂上边的Html代码,那就说明你对Html还不够熟悉,请先温习一下Html的语法。这段代码表示了一个这样的页面:



可以看到,上边的Html代码通过Form,将“题目”和“内容”传递给了页面SAVE.PHP;在SAVE.PHP中我们可以用$texttitle和$textcontent获得它们的值。
然后让我们进行第二步,“将留言保存下来”。
问题就在于如何将POST.PHP 传过来的两个变量保存下来。要想长时间的保存变量,有两种方法:保存成文件或者放入数据库里。对一个个人留言板来说,使用数据库有些大材小用的感觉,所以我们用文件的形式来保存数据。

下边是我们要用到的几个文件操作函数:

fopen
(PHP 3, PHP 4 >= 4.0.0)
fopen -- Opens file or URL
Description
int fopen ( string filename, string mode [, int use_include_path])

flock
(PHP 3>= 3.0.7, PHP 4 >= 4.0.0)
flock -- Portable advisory file locking
Description
bool flock ( int fp, int operation [, int wouldblock])


fwrite
(PHP 3, PHP 4 >= 4.0.0)

fwrite -- Binary-safe file write
Description
int fwrite ( int fp, string string [, int length])

fclose
(PHP 3, PHP 4 >= 4.0.0)

fclose -- Closes an open file pointer
Description
bool fclose ( int fp)

关于函数的具体阐述请查阅PHP手册,如果你还没有,请_blank href=http://www.php.net/distributions/manual/php_manual_en.chm>http://www.php.net/distributions/manual/php_manual_en.chm下载。现在,我们就把留言存起来:





///////////////////////////////////////////////
// This web will save the words U leave.. //
///////////////////////////////////////////////
$ft=fopen("http://www.zzchn.com/edu/20071015/words.txt","a+"); 
flock($ft,LOCK_EX); 
$data=$texttitle." ".$textcontent; 
fwrite($ft,$data); 
$ok=fclose($ft); 
if($ok) echo "save the txet successfully..."; 
?>

需要注意的是flock($ft,LOCK_EX)这一句并不是可有可无的,它在对文件进行写操作之前锁定了文件,这样就有效地避免了多个用户同时对该文件进行操作而出现的错误。看看目录里边,是不是多出来了一个名叫http://www.zzchn.com/edu/20071015/words.txt的文本文件呢?打开一看,果然是我们刚才存放的数据。
就这样,SAVE.PHP也实现了,接下来就是显示留言了,最简单地,你可以让ECHO.PHP直接显示http://www.zzchn.com/edu/20071015/words.txt,方法是使用框架或者重定向。
下面我们分别用框架和重定向的方法来实现ECHO.PHP




<-- /// ECHO.PHP: Use a fram to show the file http://www.zzchn.com/edu/20071015/words.txt 
///////////////////////////////////////////////////////////
-->



ECHO.PHP


<BR><body> <BR></html></TD></TR></TBODY></TABLE><BR>由于IE把TXT文件当作HTML文件操作,所以直接把http://www.zzchn.com/edu/20071015/words.txt当作一个框架网页就可以了。 <BR><BR>Note 有人认为这是IE的一个漏洞,因为你在网上浏览文本文件时,往往会放松警惕,不会想到文本文件居然可以和HTML文件一样,可以使用脚本来进行破坏活动。 <BR><BR>如果用重定向来实现的话就需要用到JavaSvript了: <BR><BR><TABLE style="BORDER-COLLAPSE: collapse" borderColor=#c0c0c0 cellSpacing=8 cellPadding=0 width="100%" border=1><BR><TBODY><BR><TR><BR><TD width="100%">&lt;-- <BR&gt;//////////////////////////////////////////////////////////// <BR>/// ECHO.PHP: Use Javascript to show the file http://www.zzchn.com/edu/20071015/words.txt&nbsp; <BR>//////////////////////////////////////////////////////////// <BR>--&gt; <BR></TD></TR></TBODY></TABLE><BR><P><BR>Note 当然重定向还有其他的方法,比如利用HTML文件的META标签来实现。 <BR><img SRC="http://www.zzchn.com/upimg/071015/1192391062W061F4.gif" border=0 border="0" onmousewheel="return bbimg(this)" onload="javascript:if(this.width>screen.width-600)this.style.width=screen.width-600;"> <BR>这样,我们就完成了一个最简单的留言板。</P><p><span class=yycc>源代码网整理以下</span><center></center></p></span></TD></TR> <p style="text-indent:2em"> <TR> <p style="text-indent:2em"> </font><br /> <span class=yycc>源代码网供稿.</span></td></tr></table> </div> <script type="text/javascript"><!-- google_ad_client = "pub-0717934401688269"; /* ydm8最终页标题上内容下 */ google_ad_slot = "0579630423"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> <div id="bd13"></div> <div id="next_to">上一篇: <a href=/xinwen_139989.html>程序大战 </a>&nbsp;&nbsp;下一篇: <a href=/xinwen_139991.html>C语言库函数(B类字母) </a></div> <div id="comment"> <div class="title2"> <div class="chinese2">网友评论 (0)</div> <div class="english2"><a href="#comment_set">我也要写评论</a></div> </div> <a name="comment_set"> <script src="/php/text_pl.php?id=139990" language=javascript></script> </div> <div id="relate_art"> <div class="title2"> <div class="chinese2">最简单的留言板(PHP) 的相关新闻</div> <div class="english2"></div> </div> <ul> <li><a href="/xinwen_860.html" target=_blank>谈草根站长选购服务器硬盘</a> </li><li><a href="/xinwen_862.html" target=_blank>谈草根站长选购服务器机箱</a> </li><li><a href="/xinwen_59394.html" target=_blank>linux安全常做</a> </li><li><a href="/xinwen_80783.html" target=_blank>网络应用 介绍并分析P2P流量识别与监测</a> </li><li><a href="/xinwen_80784.html" target=_blank>使用反向代理技术保护Web服务器</a> </li><li><a href="/xinwen_80785.html" target=_blank>国外空间出现乱码的几种解决办法</a> </li></ul><ul><li><a href="/xinwen_80786.html" target=_blank>另僻蹊径 通过IIS高效管理Web服务器</a> </li><li><a href="/xinwen_80787.html" target=_blank>WEB服务器流量超负载:如何分散解决</a> </li><li><a href="/xinwen_80788.html" target=_blank>使用IIS 5.0调整Web服务器的艺术与科学</a> </li><li><a href="/xinwen_80789.html" target=_blank>Windows下打造完美的服务器平台</a> </li><li><a href="/xinwen_80790.html" target=_blank>保护WEB服务器的15个技巧</a> </li><li><a href="/xinwen_80791.html" target=_blank>在WIN2000系统下安装JSP服务器</a> </li></ul><ul><li><a href="/xinwen_80792.html" target=_blank>Win Server 2003个人网络服务器安全攻略</a> </li><li><a href="/xinwen_80793.html" target=_blank>WEB专用服务器的安全设置技巧</a> </li><li><a href="/xinwen_80794.html" target=_blank>linux操作系统下的域名解析系统设置</a> </li><li><a href="/xinwen_80795.html" target=_blank>图解Web服务器和FTP服务器安全配置[1]</a> </li><li><a href="/xinwen_80796.html" target=_blank>应用:让OpenSSH成为安全的Web服务器</a> </li><li><a href="/xinwen_80797.html" target=_blank>如何在一台服务器上实现多个Web站点</a> </li></ul><ul> </ul> </div> </div> <div id="index6"> <div class="title5">会员中心</div> <div id="user"> <script src=/php/denglu.php language=javascript></script> </div> <div class="title5">服务器类</div> <div id="sort1"> <ul> <li><a href="/xinwenlei_55.html" >Windows服务器</a></li><li><a href="/xinwenlei_56.html" >Linux服务器</a></li><li><a href="/xinwenlei_109.html" >Unix</a></li><li><a href="/xinwenlei_57.html" >Mail服务器</a></li><li><a href="/xinwenlei_58.html" >Dns服务器</a></li><li><a href="/xinwenlei_60.html" >FTP服务器</a></li><li><a href="/xinwenlei_61.html" >安全防护</a></li> </ul> </div> <!-- 右侧导航广告 --> <div class="title5">本站推荐</div> <div id="sort1"> <script type="text/javascript"><!-- google_ad_client = "pub-0717934401688269"; /* 180x150, 源码最终页右侧 */ google_ad_slot = "3489132309"; google_ad_width = 180; google_ad_height = 150; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> </div> <div class="title5">服务器类之精华</div> <div id="domain"> <ul> <li><a href="/xinwen_839.html" target=_blank>安装office2003或者升级sp</a> </li><li><a href="/xinwen_818.html" target=_blank>FreeBSD关于守护进程inetd</a> </li><li><a href="/xinwen_185.html" target=_blank>IIS7.0+PHP+Mysql+zend+ph</a> </li><li><a href="/xinwen_186.html" target=_blank>图解Vista中IIS7.0的安装及</a> </li><li><a href="/xinwen_199.html" target=_blank>一个IP建多个Web站点--主机</a> </li><li><a href="/xinwen_19684.html" target=_blank>linux命令:查看及设置时间</a> </li><li><a href="/xinwen_154793.html" target=_blank>linux命令:查看所有进程ps</a> </li><li><a href="/xinwen_196.html" target=_blank>IIS出现错误后如何完全重装</a> </li><li><a href="/xinwen_840.html" target=_blank>windows桌面图标文字不透明</a> </li><li><a href="/xinwen_188.html" target=_blank>Windows Server 2008 IIS </a> </li> </ul> </div> </div> </div> <!-- 通栏广告 --> <div id="bd3"> <script type="text/javascript"><!-- google_ad_client = "pub-0717934401688269"; /* 728x90, 源码最终页底部 */ google_ad_slot = "8551407955"; google_ad_width = 728; google_ad_height = 90; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> </div> <div id="head2_1_result" style="display:none;"> <div id="menu1"> <div id="menu_tab1" class="menu_bg2" onmouseover=menu(1) >&nbsp;&nbsp;<a href="/">首页</a>&nbsp;</div> <div class="menu_tab0"></div> <div id="menu_tab2" class="menu_bg1" onmouseover=menu(2) ><a href="/xinwenlei_2.html">新闻资讯</a></div> <div class="menu_tab0"></div> <div id="menu_tab3" class="menu_bg1" onmouseover=menu(3) ><a href="/xinwenlei_3.html">网站运营</a></div> <div class="menu_tab0"></div> <div id="menu_tab4" class="menu_bg1" onmouseover=menu(4) ><a href="/xinwenlei_4.html">网络编程</a></div> <div class="menu_tab0"></div> <div id="menu_tab5" class="menu_bg1" onmouseover=menu(5) ><a href="/xinwenlei_5.html">设计在线</a></div> <div class="menu_tab0"></div> <div id="menu_tab6" class="menu_bg1" onmouseover=menu(6) ><a href="/xinwenlei_7.html">服务器类</a></div> <div class="menu_tab0"></div> <div id="menu_tab7" class="menu_bg1" onmouseover=menu(7) ><a href="http://www.ydm8.com/yuming/" target=_blank>域名空间</a></div><div class="menu_tab0"></div> <div id="menu_tab8" class="menu_bg1" onmouseover=menu(8) ><a href="http://tool.ydm8.com/" target=_blank>站长工具</a></div><div class="menu_tab0"></div> <div id="menu_tab9" class="menu_bg1" onmouseover=menu(9) ><a href="http://soft.ydm8.com" target=_blank>软件下载</a></div><div class="menu_tab0"></div> <div id="menu_tab10" class="menu_bg1" onmouseover=menu(10) ><a href="/user/login.php" target=_blank>我的源代码</a></div><div class="menu_tab0"></div> </div> <div id="menu_con1" class="menu2"> <form action="/sousuo.html" name="sForm" id="sForm" method="get"> <a href="http://www.soft.ydm8.com/soft.html" target=_blank><b><u>点击查看所有软件分类</u></b></a> | 源代码网搜索:<input type="text" id="kw" name="key" size="45" maxlength="100" value="" /> <input type="submit" value="内容搜索" /></td> <span><b>热门搜索:</b></span><a href="/sousuo.html?key=asp" target=_blank>asp</a><a href="/sousuo.html?key=php" target=_blank>php</a><a href="/sousuo.html?key=jsp" target=_blank>jsp</a> </form> </div> <div id="menu_con2" class="menu3"> <a href="/xinwenlei_91.html">站长新闻</a>| <a href="/xinwenlei_92.html">业界动态</a>| <a href="/xinwenlei_93.html">收购融资</a>| <a href="/xinwenlei_94.html">门户动态</a>| <a href="/xinwenlei_95.html">搜索引擎</a>| <a href="/xinwenlei_96.html">网络游戏</a>| <a href="/xinwenlei_97.html">电子商务</a>| <a href="/xinwenlei_98.html">广告传媒</a>| <a href="/xinwenlei_99.html">厂商开发</a>| <a href="/xinwenlei_100.html">访谈</a>| <a href="/xinwenlei_104.html">站长休闲</a>| </div> <div id="menu_con3" class="menu3"> <a href="/xinwenlei_86.html">建站运营</a>| <a href="/xinwenlei_87.html">策划盈利</a>| <a href="/xinwenlei_88.html">seo搜索优化</a>| <a href="/xinwenlei_89.html">网站推广</a>| <a href="/xinwenlei_90.html">免费资源</a>| <a href="/xinwenlei_101.html">经验心得</a>| <a href="/xinwenlei_103.html">域名</a>| <a href="/xinwenlei_105.html">广告联盟</a>| <a href="/xinwenlei_110.html">站长入门</a>| </div> <div id="menu_con4" class="menu3"> <a href="/xinwenlei_9.html">软件语言</a>| <a href="/xinwenlei_11.html">数据库</a>| <a href="/xinwenlei_12.html">WEB编程</a>| <a href="/xinwenlei_13.html">软件开发</a>| <a href="/xinwenlei_14.html">IT认证</a>| <a href="/xinwenlei_102.html">新手教程</a>| </div> <div id="menu_con5" class="menu3"> <a href="/xinwenlei_51.html">网页设计</a>| <a href="/xinwenlei_52.html">平面动画</a>| <a href="/xinwenlei_53.html">设计作品</a>| <a href="/xinwenlei_54.html">程序应用</a>| </div> <div id="menu_con6" class="menu3"> <a href="/xinwenlei_55.html">Windows服务器</a>| <a href="/xinwenlei_56.html">Linux服务器</a>| <a href="/xinwenlei_109.html">Unix</a>| <a href="/xinwenlei_57.html">Mail服务器</a>| <a href="/xinwenlei_58.html">Dns服务器</a>| <a href="/xinwenlei_60.html">FTP服务器</a>| <a href="/xinwenlei_61.html">安全防护</a>| </div> <div id="menu_con7" class="menu3"> </div> <div id="menu_con8" class="menu3"> &nbsp;&nbsp;&nbsp;<a href="http://tool.ydm8.com/baidu/" target=_blank><u>百度每日收录查询</u></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="http://tool.ydm8.com/google/" target=_blank><u>GOOGLE每日收录查询</u></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="http://tool.ydm8.com/baidu/key/" target=_blank><u>关键词百度排名查询</u></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="http://tool.ydm8.com/google/key/" target=_blank><u>关键词GOOGLE排名查询</u></a> </div> <div id="menu_con9" class="menu3"> <a href="http://soft.ydm8.com/softlei_7.html">网络软件</a>| <a href="http://soft.ydm8.com/softlei_8.html">系统工具</a>| <a href="http://soft.ydm8.com/softlei_9.html">应用软件</a>| <a href="http://soft.ydm8.com/softlei_16.html">联络聊天</a>| <a href="http://soft.ydm8.com/softlei_17.html">图形图像</a>| <a href="http://soft.ydm8.com/softlei_18.html">多媒体类</a>| <a href="http://soft.ydm8.com/softlei_19.html">行业软件</a>| <a href="http://soft.ydm8.com/softlei_20.html">游戏娱乐</a>| <a href="http://soft.ydm8.com/softlei_21.html">编程开发</a>| <a href="http://soft.ydm8.com/softlei_22.html">安全相关</a>| <a href="http://soft.ydm8.com/softlei_23.html">教育教学</a>| <a href="http://soft.ydm8.com/softlei_178.html">手机软件</a>| </div> <div id="menu_con10" class="menu3"> <font color=#000000>从源代码网这里可以发表您自己喜欢的原创文章,同时也希望您的发表能给很多人带来帮助。</font> </div> </div> <script language="javascript"> document.getElementById("head2_1").innerHTML = document.getElementById("head2_1_result").innerHTML; </script> <!--底部--> <div id="foot"> <a href="http://www.ydm8.com" title="源代码网" target="_blank"><span>源代码网</span></a><span>版权所有 2008</span> <a href="http://www.ydm8.com" title="源代码网"><span>YDM8.COM</span></a> <a href="http://www.miibeian.gov.cn/" target="_blank"><span>京ICP备08100796号</span></a> </div> <span style=display:none><script language="javascript" type="text/javascript" src="http://js.users.51.la/2317140.js"></script> <noscript><a href="http://www.51.la/?2317140" target="_blank"><img alt="&#x6211;&#x8981;&#x5566;&#x514D;&#x8D39;&#x7EDF;&#x8BA1;" src="http://img.users.51.la/2317140.asp" style="border:none" /></a></noscript></div> <script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); </script> <script type="text/javascript"> try { var pageTracker = _gat._getTracker("UA-5953062-2"); pageTracker._trackPageview(); } catch(err) {}</script></body> </html>