php+dbfile开发小型留言本


Posted in PHP onOctober 09, 2006

最近一直在用php+dbfile开发blog,开发过程中学到了不少东西,于是就试着写了一个小留言本。
这个留言本采用php+dbfile,不需要使用数据库,可以放在blog中使用,比如http://www.customyze.com,这个blog中的Tag Board就是这个留言本。

整个留言本需要四个文件,分别是:board.php、index.php、config.php、admin.php。

board.php用来存储数据,可以先在里面添加了一条留言纪录。 代码拷贝框
<?php $Board=array( array(1081410332,'测试','测试留言本','http://www.piscdong.com') ); ?>
[Ctrl+A 全部选择 然后拷贝]

index.php是留言显示和提交页面。 代码拷贝框
<?php require_once('board.php'); function htmlencode($content){ $content=htmlspecialchars($content); $content=preg_replace("/\r/i","<br />",$content); return $content; } if($HTTP_SERVER_VARS['REQUEST_METHOD']=='POST'){ $configpath_parts1 = pathinfo($SCRIPT_FILENAME); $time=time(); $name=$HTTP_POST_VARS['name']; $url=(preg_match("/^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$/i",$HTTP_POST_VARS['url'])
$HTTP_POST_VARS['url']=='')?$HTTP_POST_VARS['url']:'http://'.htmlspecialchars(preg_replace("/https?\:\/\//i",'',$HTTP_POST_VARS['url']),ENT_QUOTES); $info=htmlencode($HTTP_POST_VARS['info']); if($name!='' && $info!=''){ $Board[]=array($time,$name,$info,$url); } for($i=0;$i<count($Board);$i++){ $bd=current($Board); $s[]="\tarray(".$bd[0].",'".$bd[1]."','".$bd[2]."','".$bd[3]."')"; next($Board); } $content="<?php\n\$Board=array(\n".join($s,",\n")."\n);\n?>"; $filename=$configpath_parts1['dirname'].'/'.'board.php'; if(is_writable($filename)
!file_exists($filename)){ if(!$handle=fopen($filename,'w')){ return false; } if(!fwrite($handle,$content)){ return false; } fclose($handle); }else{ return false; } header('Location:.'); }else{ ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>留言本</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> </head> <body> <form method="post" name="form1" action=""> <table border="0" cellspacing="5" cellpadding="0" align="center"> <tr> <td> <div style="overflow:auto;height:250px;border:1px dotted #999999;padding:5px;word-wrap:break-word;width:400px;"> <?php end($Board); for($i=0;$i<count($Board);$i++){ $bd=current($Board); $s[]='<strong>'.($bd[3]!=''?'<a href="':'').(preg_match("/^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$/i",$bd[3])?'mailto:':'').$bd[3].(($bd[3]!='' && !preg_match("/^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$/i",$bd[3]))?'" target="_blank':'').($bd[3]!=''?'">':'').$bd[1].($bd[3]!=''?'</a>':'').':</strong> '.$bd[2].'<br/><em>-'.date("G:i, M j, Y",$bd[0]).'</em>'; prev($Board); } echo join($s,'<br/><br/>'); ?> </div> </td> </tr> <tr> <td align="center"> 名称:<input type="text" name="name"/> URL/Email:<input type="text" name="url"/><br/> <textarea name="info" cols="40" rows="8"></textarea><br/> <input type="submit" value="发布"/> </td> </tr> </table> </form> </body> </html> <?php } ?>
[Ctrl+A 全部选择 然后拷贝]

config.php中存放的是管理留言本的密码,把密码放在单独一个文件中方便修改。 代码拷贝框
<?php $password='123456'; ?>
[Ctrl+A 全部选择 然后拷贝]

admin.php是管理页面,功能很简单,只能删除留言。在删除时需要输入管理密码,管理密码存放在config.php中。 代码拷贝框
<?php require_once('board.php'); require_once('config.php'); if(isset($HTTP_POST_VARS['id']) && $HTTP_POST_VARS['id']!='' && addslashes($HTTP_POST_VARS['password'])==$password){ if(count($Board)>1){ unset($Board[intval($HTTP_POST_VARS['id'])]); for($i=0;$i<count($Board);$i++){ $bd=current($Board); $s[]="\tarray(".$bd[0].",'".$bd[1]."','".$bd[2]."','".$bd[3]."')"; next($Board); } $content="<?php\n\$Board=array(\n".join($s,",\n")."\n);\n?>"; $configpath_parts1 = pathinfo($SCRIPT_FILENAME); $filename=$configpath_parts1['dirname'].'/'.'board.php'; if(is_writable($filename)
!file_exists($filename)){ if(!$handle=fopen($filename,'w')){ return false; } if(!fwrite($handle,$content)){ return false; } fclose($handle); }else{ return false; } } header('Location:admin.php'); }else{ ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>管理留言本</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> </head> <body> <table width="500" border="0" cellspacing="1" cellpadding="5" align="center" bgcolor="#999999"> <?php for($i=0;$i<count($Board);$i++){ $bd=current($Board); $s[]='<tr><td bgcolor="#'.($i%2!=0?'ececec':'ffffff').'"><strong>'.($bd[3]!=''?'<a href="':'').(preg_match("/^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$/i",$bd[3])?'mailto:':'').$bd[3].(($bd[3]!='' && !preg_match("/^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$/i",$bd[3]))?'" target="_blank':'').($bd[3]!=''?'">':'').$bd[1].($bd[3]!=''?'</a>':'').':</strong> '.$bd[2].'<br/><em>-'.date("G:i, M j, Y",$bd[0]).'</em></td>'.(count($Board)>1?'<td bgcolor="#'.($i%2!=0?'ececec':'ffffff').'" align="center"><form method="post" action=""><input type="submit" value="删除" /><input type="hidden" name="id" value="'.$i.'" /><input type="password" name="password" /></form></td>':'').'</tr>'; next($Board); } echo join($s,''); ?> </table> </body> </html> <?php } ?>
[Ctrl+A 全部选择 然后拷贝]

这个留言本还很简单,功能上还不健全,比如没有分页等,还可以继续完善。:-)

PHP 相关文章推荐
PHP新手上路(十二)
Oct 09 PHP
Ajax+PHP 边学边练 之二 实例
Nov 24 PHP
php中防止SQL注入的最佳解决方法
Apr 25 PHP
PHP使用CURL获取302跳转后的地址实例
May 04 PHP
PHP+Mysql+Ajax+JS实现省市区三级联动
May 23 PHP
yii实现级联下拉菜单的方法
Jul 31 PHP
thinkphp中空模板与空模块的用法实例
Nov 26 PHP
PHP时间和日期函数详解
May 08 PHP
PHP学习笔记之php文件操作
Jun 03 PHP
ThinkPHP模板标签eq if 中区分0,null,false的方法
Mar 24 PHP
PHP 命名空间和自动加载原理与用法实例分析
Apr 29 PHP
PHP实现简易用户登录系统
Jul 10 PHP
用 php 编写的日历
Oct 09 #PHP
第十三节 对象串行化 [13]
Oct 09 #PHP
第七节 类的静态成员 [7]
Oct 09 #PHP
用PHP实现文件上传二法
Oct 09 #PHP
PHP安装全攻略:APACHE
Oct 09 #PHP
用PHP生成自己的LOG文件
Oct 09 #PHP
VFP与其他应用程序的集成
Oct 09 #PHP
You might like
Discuz! Passport 通行证整合
2008/03/27 PHP
php生成excel列名超过26列大于Z时的解决方法
2014/12/29 PHP
zend framework重定向方法小结
2016/05/28 PHP
功能强大的php文件上传类
2016/08/29 PHP
PHP中SESSION过期设置
2021/03/09 PHP
自己整理的一个javascript日期处理函数
2010/10/16 Javascript
jquery 插件学习(二)
2012/08/06 Javascript
改变文件域的样式实现思路同时兼容ie、firefox
2013/10/23 Javascript
setInterval计时器不准的问题解决方法
2014/05/08 Javascript
jQuery.prop() 使用详解
2015/07/19 Javascript
纯JS实现表单验证实例
2016/12/24 Javascript
基于jQuery制作小图标上下滑动特效
2017/01/18 Javascript
JS实现的简单表单验证功能完整实例
2017/10/14 Javascript
vue2.0s中eventBus实现兄弟组件通信的示例代码
2017/10/25 Javascript
javascript实现数字配对游戏的实例讲解
2017/12/14 Javascript
JS实现简单的浮动碰撞效果示例
2017/12/28 Javascript
JS实现字符串去重及数组去重的方法示例
2018/04/21 Javascript
WebSocket的通信过程与实现方法详解
2018/04/29 Javascript
vant-ui框架的一个bug(解决切换后onload不触发)
2020/11/11 Javascript
python进阶教程之模块(module)介绍
2014/08/30 Python
Python实现把回车符\r\n转换成\n
2015/04/23 Python
python实现搜索本地文件信息写入文件的方法
2016/02/22 Python
一个基于flask的web应用诞生 flask和mysql相连(4)
2017/04/11 Python
Flask之flask-script模块使用
2018/07/26 Python
基于python实现KNN分类算法
2020/04/23 Python
Python3使用Matplotlib 绘制精美的数学函数图形
2019/04/11 Python
Python代码块及缓存机制原理详解
2019/12/13 Python
python装饰器相当于函数的调用方式
2019/12/27 Python
tensorflow 实现自定义梯度反向传播代码
2020/02/10 Python
创意爱尔兰礼物:Creative Irish Gifts
2020/01/29 全球购物
工程开工庆典邀请函
2014/02/01 职场文书
机电一体化求职信
2014/03/10 职场文书
求职意向书范文
2014/04/01 职场文书
护士节演讲稿开场白
2014/08/25 职场文书
2015新学期家长寄语
2015/02/26 职场文书
通知范文怎么写
2015/04/16 职场文书