老师,为什么我留不了言,帮忙帮我找一下,谢谢
来源:1-2 留言板功能实现
mengzezheng
2019-08-03 10:52:34
<?php
header('content-type:text/html;charset=utf-8');
date_default_timezone_get('PRC');
$filename='msg.txt';
$msgs=[];
//检测文件是否存在
if(file_exists($filename)){
$string=file_get_contents($filename);
//读取文件中的内容
if(strlen($string)>0){//内容是否存在
$msgs=unserialize($string);
}
}
//检测用户是否点击了提交按钮
if(isset($_POST['putmsgs'])){
$username=$_POST['username'];
$title=strip_tags($_POST['title']);//防止html语言攻击
$content=strip_tags($_POST['content']);
$time=time();
//将其组成关联数组
$data=compact($username,$title,$content,$time);
array_push($msgs,$data);
$msgs=serialize($msgs);
if(file_get_contents($filename,$msgs)){
echo "<script>alert('留言成功'); location.href='comments.php'</script>";
}
else{
echo "<script>alert('留言失败'); location.href='comments.php'</script>";
}
}
//print_r($msgs);
/*
*二维索引数组: $msgs=[
* [...],
* [...]
* ];
* file_get_contents($filename)得到文件中的内容,返回的是字符串
* file_put_contents($filename,$data)向指定文件写内容,如果文件不存在会创建
* serialize($str)序列化字符串
* unserialize($str)反序列化
*/
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>社区</title>
<script type="text/javascript" src="http://www.francescomalagrino.com/BootstrapPageGenerator/3/js/jquery-2.0.0.min.js"></script>
<script type="text/javascript" src="http://www.francescomalagrino.com/BootstrapPageGenerator/3/js/jquery-ui"></script>
<link href="http://www.francescomalagrino.com/BootstrapPageGenerator/3/css/bootstrap-combined.min.css" rel="stylesheet" media="screen">
<script type="text/javascript" src="http://www.francescomalagrino.com/BootstrapPageGenerator/3/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container-fluid">
<div class="row-fluid">
<div class="span12">
<div class="page-header">
<h1>
<strong>砰砰唰交流社区</strong>
</h1>
</div>
<div class="hero-unit">
<h1>
Hello, world!
</h1>
<p>
这是一个可视化布局模板, 你可以点击模板里的文字进行修改, 也可以通过点击弹出的编辑框进行富文本修改. 拖动区块能实现排序.
</p>
</div>
<?php
if(is_array($msgs)&&count($msgs)>0){
?>
<table class="table table-hover">
<thead>
<tr>
<th>
编号
</th>
<th>
用户名
</th>
<th>
标题
</th>
<th>
时间
</th>
<th>
内容
</th>
</tr>
</thead>
<tbody>
<?php $i=1; foreach ($msgs as $val){?>
<tr class="success">
<td>
<?php echo $i++;?>
</td>
<td>
<?php echo $val['username'];?>
</td>
<td>
<?php echo $val['title'];?>
</td>
<td>
<?php echo date("y-m-d h:i:s",$val['time']);?>
</td>
<td>
<?php echo $val['content'];?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php }?>
<form action="#" method="post">
<fieldset>
<legend>留言区</legend>
<label>用户名</label><input type="text" name="username" required><!--required为必填项-->
<label>标题</label><input type="text" name="title" required>
<label>内容</label><textarea name="content" rows="5" cols="30" required></textarea>
<hr>
<input class="btn btn-primary btn-lg" type="submit" name="putmsgs" value="发布留言">
</fieldset>
</form>
</div>
</div>
</div>
</body>
</html>
1回答
好帮手慕查理
2019-08-03
您好,1.compact()函数的参数错误。
2.写入文件应该是file_put_contents。

祝学习愉快!
相似问题