最近一直在用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 全部选择 然后拷贝]
这个留言本还很简单,功能上还不健全,比如没有分页等,还可以继续完善。:-)
如对本文有疑问,
点击进行留言回复!!
相关文章:
-
-
-
-
-
-
-
-
-
管理后台无限级菜单
管理后台菜单遍历很久就想做一个通用的管理后台出来,一直太懒没做,今天开始粗略搞了一下,只是先把框架搭建好,菜单栏...
[阅读全文]
-
桥接模式学习总结
桥接模式引言假如一个系统需要加入报表模块,主要的开发点是数据的获取和报表的显示方式。数据的获取可能是从系统数据库...
[阅读全文]
-
网友评论