美文网首页
php学习-简易留言板

php学习-简易留言板

作者: 发呆发呆 | 来源:发表于2017-09-03 14:06 被阅读0次

①首页:

<?php
header('content-type:text/html;charset=utf-8');
date_default_timezone_set('PRC');
$filename="msg.txt";
$msgs=[];
//检测文件是否存在
if(file_exists($filename)){
//读取文件中的内容
    $string=file_get_contents($filename);
    if(strlen($string)>0){
        $msgs=unserialize($string);//反序列化

    }
}
//检测用户是否点击了提交按钮
if(isset($_POST['pubMsg'])){
    $username=$_POST['username'];
    $title=strip_tags($_POST['title']);//从字符串中去除 HTML 和 PHP 标记
    $content=strip_tags($_POST['content']);
    $time=time();
//将其组成关联数组
    $data=compact('username','title','content','time');
    //判断是编辑留言还是添加留言
    if(isset($_POST['editkey'])){
        $key=$_POST['editkey'];
        $msgs[$key]=$data;
    }
    else{
        array_push($msgs,$data);}
    $msgs=serialize($msgs);
  if(file_put_contents($filename,$msgs)) { //将一个字符串写入文件
      if (isset($_POST['editkey'])) {
          echo "<script>alert('编辑成功!');location.href='index.php';</script>";
      } else {
          echo "<script>alert('留言成功!');location.href='index.php';</script>";
      }
  }
      else{
      if(isset($_POST['editkey'])) {
          echo "<script>alert('编辑失败,请重试!');location.href='index.php';</script>";
      }else{
          echo "<script>alert('留言失败,请重试!');location.href='index.php';</script>";
          }
      }
}
/*
$msgs=[
[...],
[...]
];
file_get_contents($filename);得到文件中的内容,返回的是字符串
file_put_contents($filename,$data);向指定文件写内容,如果文件不存在,会创建
serialize($str);序列化字符串
unserialize($str);反序列化
*/
//print_r($msgs);
?>
<!DOCTYPE html>
<html>
<head>
    <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>
                    留言板-<small>V1.0</small>
                </h1>
            </div>
            <div class="hero-unit">
                <h1>
                    Hello
                </h1>
                <p>
                    来了就说二句吧~~~
                </p>
            </div>
            <?php if(is_array($msgs)&&count($msgs)>0):?>
                <table class="table">
                    <thead>
                    <tr>
                        <th>
                            编号
                        </th>
                        <th>
                            用户
                        </th>
                        <th>
                            标题
                        </th>
                        <th>
                            时间
                        </th>
                        <th>
                            内容
                        </th>
                        <th>
                            操作
                        </th>
                    </tr>
                    </thead>
                    <body>
                    <?php $i=1;foreach($msgs as $key=>$val):?>
                        <tr class="success">
                            <td>
                                <?php echo $key++;?>
                            </td>
                            <td>
                                <?php echo $val['username'];?>
                            </td>
                            <td>
                                <?php echo $val['title'];?>
                            </td>
                            <td>
                                <?php echo date("m/d/Y H:i:s",$val['time']);?>
                            </td>
                            <td>
                                <?php echo $val['content'];?>
                            </td>
                            <td>
                                <a href="edit.php?editkey=<?php echo $key?>">编辑</a>|
                                <a href="delete.php?delkey=<?php echo $key?>">删除</a>
                            </td>
                        </tr>
                    <?php endforeach;?>
                    </body>
                </table>
            <?php endif;?>
               <input type="submit" class="btn btn-primary btn-lg" value="我要留言" onclick="window.location.href='add.php'"/>
            <hr/>
        </div>
    </div>
</div>
</body>
</html>

②增加留言:

<?php
header('content-type:text/html;charset=utf-8');
date_default_timezone_set('PRC');
?>
<!DOCTYPE html>
<html>
<head>
    <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>
                    留言板-<small>V1.0</small>
                </h1>
            </div>
            <div class="hero-unit">
                <h1>
                    Hello
                </h1>
                <p>
                      来了就说二句吧~~~
                </p>
            </div>
            <div style="width: 100%;text-align:center">
            <form class="form-horizontal center-block" action="index.php" method="post">
                <fieldset>
                    <legend>请留言</legend>
                    <label>用户名</label><input style="" type="text" name="username" required/>
                    <label>标题</label><input style="" type="text" name="title" required/>
                    <label>内容</label><textarea name="content" rows="10" cols="30"></textarea>
                    <hr/>
                    <input type="submit" class="btn btn-primary btn-lg" name="pubMsg" value="发布留言" />
                    <input type="submit" class="btn btn-default btn-lg" value="查看留言" onclick="window.location.href='index.php'"/>
                </fieldset>
            </form>
            </div>
        </div>
    </div>
</div>
</body>
</html>

③编辑留言:

<?php
header('content-type:text/html;charset=utf-8');
date_default_timezone_set('PRC');
$filename="msg.txt";
$msgs=array();
if (isset($_GET['editkey'])) {
    $editkey = $_GET['editkey'];//接收id
}
//检测文件是否存在
if(file_exists($filename)) {
//读取文件中的内容
    $string = file_get_contents($filename);
    if (strlen($string) > 0) {
        $msgs = unserialize($string);
    }
    $editarr=$msgs[$editkey];
}else{
    echo '文件不存在';
}
?>
<!DOCTYPE html>
<html>
<head>
    <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>
                    留言板-<small>V1.0</small>
                </h1>
            </div>
            <div class="hero-unit">
                <h1>
                    Hello
                </h1>
                <p>
                  你可以修改你的留言
                </p>
            </div>
            <div style="width: 100%;text-align:center">
                <form class="form-horizontal center-block" action="index.php" method="post">
                    <fieldset>
                        <legend>编辑</legend>
                        <label>用户名</label><input type="text" name="username" value="<?php echo $editarr['username'];?>" required/>
                        <label>标题</label><input  type="text" name="title" value="<?php echo $editarr['title'];?>" required/>
                        <label>内容</label><textarea name="content" rows="10" cols="30" ><?php echo $editarr['content'];?></textarea>
                        <hr/>
                        <input type="submit" class="btn btn-primary btn-lg" name="pubMsg" value="编辑留言" />
                        <input type="submit" class="btn btn-default btn-lg" value="查看留言" onclick="window.location.href='index.php'">
                    </fieldset>
                    <input type="hidden" name="editkey" value="<?php echo $editkey?>"/>
                </form>
            </div>
        </div>
    </div>
</div>
</body>
</html>

④删除留言:

<?php
header('content-type:text/html;charset=utf-8');
date_default_timezone_set('PRC');
$filename='msg.txt';
$msgs=array();
if(isset($_GET['delkey'])&&!empty($_GET['delkey'])){
    $delkey=$_GET['delkey']-1;
}
if(file_exists($filename)) {
    $string = file_get_contents($filename);//获取文本内容
    if (strlen($string) > 0) {
        $msgs = unserialize($string);//反序列化赋值
    }
}
else{
    echo '文件不存在';
}
if(!isset($msgs[$delkey])){
    echo "<script>alert('数据出错,请重试!');location.href='index.php';</script>";
}
unset($msgs[$delkey]);//销毁指定的变量
$msgs=serialize($msgs);
if(file_put_contents($filename,$msgs)){//将一个字符串写入文件
    echo "<script>alert('删除成功!');location.href='index.php';</script>";
}else{
    echo "<script>alert('删除失败,请重试!');location.href='index.php';</script>";
}
?>

相关文章

网友评论

      本文标题:php学习-简易留言板

      本文链接:https://www.haomeiwen.com/subject/mxfjjxtx.html