//发送留言
require("global.func.php教程");
$fid=isset($_GET["id"])?intval($_GET["id"]):0;
$page=isset($_GET["page"])?intval($_GET["page"]):1;
if($_SERVER["REQUEST_METHOD"]=="POST")
{
if(!file_exists("1.php"))
{
writeFile("1.php",$array=array());
}
$arr=require("1.php");
$post=array();
unset($_POST["Submit"]);
foreach($_POST as $k=>$v)
{
$post[$k]=addslashes(htmlspecialchars($v));
}
$post["content"]=str_replace(" ","<br>",$post["content"]);
$post["id"]=count($arr)+1;
$post["date"]=time();
$post["fid"]=$fid;
$post["ip"]=$_SERVER["REMOTE_ADDR"];
$arr[]=$post;
writeFile("1.php",$arr);
echo "<script>location.href="index.php?page=$page"</script>";
exit;
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>发帖</title>
<script>
var arr=new Array();
function ckusername()
{
if(document.form1.username.value.length<3)
{
document.getElementById("user_msg").innerHTML="<font color="red"><b>用户名不能小于3个字符</b></font>";
arr[0]=1;
}else
{
document.getElementById("user_msg").innerHTML="<font color="green"><b>√</b></font>";
arr[0]=0;
}
}
function ckcontent()
{
if(document.form1.content.value.length<8||document.form1.content.value.length>255)
{
document.getElementById("content_msg").innerHTML="<font color="red"><b>内容8-255个字符</b></font>";
arr[1]=1;
}else
{
document.getElementById("content_msg").innerHTML="<font color="green"><b>√</b></font>";
arr[1]=0;
}
}
function c()
{
if(arr.length<2)
{
return false;
}
for(x in arr)
{
if(arr[x]==1)
{
return false;
}
}
form1.submit();
}
</script>
</head>
<body>
<form name="form1" method="post" action="">
<table width="100%" border="0">
<tr>
<td>用户
<input name="username" type="text" id="id" onBlur="ckusername()" ></td>
</tr>
<tr>
<td><div id="user_msg"></div>
</td>
</tr>
<tr>
<td><textarea name="content" cols="100" rows="6" id="content" onBlur="ckcontent()"></textarea></td>
</tr>
<tr>
<td><div id="content_msg"></div>
</td>
</tr>
<tr>
<td><div align="center">
<input type="button" name="Submit" value="提交" onClick="c();">
</div></td>
</tr>
</table>
</form>
</body>
</html>
<?php
ini_set("display_errors",0);
require("global.func.php");
?>
显示像163评论
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.111cn.net/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
.b{
border:1px dotted #e3e3e3;
word-wrap:break-word;
word-break:break-all;
}
</style>
<title>PPC备用论坛(递归限制15层+主题)</title>
</head>
<body>
<?php
echo "<a href="post.php?page=1"> 发帖</a><br>";
if(file_exists("1.php"))
{
$__array=require("1.php");
}
else
{
$__array=array();
}
$page=isset($_GET["page"])?intval($_GET["page"]):1;
$pagesize=5;
$offset=($page-1)*$pagesize;
$__str="";
function getTotal()
{
global $__array;
$i=0;
foreach($__array as $k=>$v)
{
if($v["fid"]==0)
{
$i=$i+1;
}
}
return $i;
}
function getFid0()
{
global $__array,$__str;
foreach ($__array as $k=>$v)
{
if($v["fid"]==0)
{
$fid0[]=$v;
}
}
return array_reverse($fid0);
}
$fid0=getFid0();
function show($fid0,$pagesize,$offset)
{
global $__str,$page;
foreach (array_slice($fid0,$offset,$pagesize,true) as $k=>$v)
{
$__str.="<blockquote class="b"><p>";
$__str.=$v["ip"]."用户:".$v["username"]."内容:".$v["content"]."<a id="a1" href="post.php?id=".$v["id"]."&page=$page">回复</a>";
showChild($v["id"]);
$__str.="</p></blockquote>";
}
}
function showChild($id,$i=0)
{
global $__array,$__str,$page,$offset;
if($i==15)//递归层数
{
return;
}
$i++;
foreach($__array as $k=>$v)
{
if($v["fid"]==$id)
{
$__str.= "<blockquote class="b"><p id="a1">";
$__str.= $__array[$k]["ip"]."用户:".$__array[$k]["username"]."内容:".$__array[$k]["content"]."<a href="post.php?id=".$v["id"]."&page=$page">回复</a>";
showChild($v["id"],$i);
$__str.= "</p></blockquote>";
}
}
}
show($fid0,$pagesize,$offset);
$total=getTotal();
echo $__str;
pageft($total,$pagesize,"index.php");
echo "<br>".$pagenav;
?>
</body>
</html>
global.func.php文件代码如下
<?php
function writeFile($id,$array)
{
$content="<?php return ".var_export($array,true)."?>";
return safe_file_put_contents($id,$content);
}
/**
*写入文件
*@param string $filename
*@param string $contents
*@return boolean
*/
function safe_file_put_contents($filename,&$contents)
{
$fp=fopen($filename,"wb");
if($fp)
{
flock($fp,LOCK_EX);
fwrite($fp,$contents);
flock($fp,LOCK_UN);
fclose($fp);
return true;
}else
{
return false;
}
}
/**
*读取文件
*@param string $filename
*@return $data|false
*/
function safe_file_get_contents($filename)
{
$fp=fopen($filename,"rb");
if($fp)
{
flock($fp,LOCK_SH);
clearstatcache();
$filesize=filesize($filename);
if($filesize>0)
{
$data=fread($fp,$filesize);
}else
{
$data=false;
}
flock($fp,LOCK_UN);
fclose($fp);
return $data;
}else
{
return false;
}
}
function pageft($total,$displaypg=10,$url=''){
global $page,$firstcount,$pagenav,$_SERVER;
$GLOBALS["displaypg"]=$displaypg;
if(!$page) $page=1;
if(!$url){ $url=$_SERVER["REQUEST_URI"];}
$parse_url=parse_url($url);
@$url_query=$parse_url["query"];
if($url_query){
$url_query=ereg_replace("(^|&)page=$page","",$url_query);
$url=str_replace($parse_url["query"],$url_query,$url);
if($url_query) $url.="&page"; else $url.="page";
}else {
$url.="?page";
}
$lastpg=ceil($total/$displaypg);
$page=min($lastpg,$page);
$prepg=$page-1;
$nextpg=($page==$lastpg ? 0 : $page+1);
if(!$page) $page=1;
$firstcount=($page-1)*$displaypg;
$pagenav.=" <a href='$url=1' class='s1'>首页</a> ";
if($prepg) $pagenav.=" <a href='$url=$prepg' class='s1'>上页</a> "; else $pagenav.="上页 ";
if($nextpg) $pagenav.=" <a href='$url=$nextpg' class='s1'>下页</a> "; else $pagenav.="下页 ";
$pagenav.=" <a href='$url=$lastpg' class='s1'>尾页</a>";
$pagenav.=" 到 <select name='topage' size='1' onchange='window.location="$url="+this.value'> ";
for($i=1;$i<=$lastpg;$i++){
if($i==$page) $pagenav.="<option value='$i' selected>$i</option> ";
else $pagenav.="<option value='$i'>$i</option> ";
}
$pagenav.="</select> 页";
}
?>
//保存的1.php文件数数组形式,如下格式
<?php return array (
0 =>
array (
'username' => 'test',
'content' => '111cn.net',
'id' => 1,
'date' => 1253853347,
'fid' => 0,
'ip' => '116.226.2.200',
),
1 =>
array (
'username' => 'aaaaaaaaaaad',
'content' => 'www.111cn.net',
'id' => 2,
'date' => 1253857672,
'fid' => 0,
'ip' => '116.226.6.188',
),
)?>