首页 > php教程 > php手册 > 正文

PHP实例一之简单的留言板

php中文网
发布: 2016-06-06 19:59:51
原创
1808人浏览过

注:参考兄弟连教学视频写的一个简单的留言板模块,获得更多内容请参考我的博文。 示例:文本式留言板 需要的知识点: 1.文件操作: file_put_contents();文件的写入函数 file_get_contents();文件的读取函数 2.字符串的处理函数 explode();将字串拆分成数组

注:参考兄弟连教学视频写的一个简单的留言板模块,获得更多内容请参考我的博文。

示例:文本式留言板
需要的知识点:
  1.文件操作:
   file_put_contents();文件的写入函数
   file_get_contents();文件的读取函数
  2.字符串的处理函数
   explode();将字串拆分成数组的函数
   implode();将数组以指定分割符合并成字串的函数
  3.数组
     foreach() 遍历数组
     unset()  销毁变量
     全局数组:
     $_POST[]
     $_SERVER["REMOTE_ADDR"];//获取客户端的IP地址
     time();//获取当前系统的时间戳
     date();日期转换函数;
示例的目录结构:
====================================
|--index.php 添加留言信息界面
|
|--doAdd.php获取留言信息,并执行添加操作的php文件
|
|--show.php显示留言信息的界面
|
|--del.php 执行删除留言信息的界面
|
|--liuyan.txt 用于储存留言信息的文件

代码实现:

index.php部分:

立即学习PHP免费学习笔记(深入)”;

换物网站源码
换物网站源码

一个基于ASP.NET+MSSQL实现的网站源码,包含一个网站的后台管理、前面展示、留言等常用功能,简单而功能完整,具有相当的学习意义。 采用面向对象模式开发,暂时没有超级管理员管理后台

换物网站源码 0
查看详情 换物网站源码
<html>
<head>
<title>我的留言板</title>
</head>
<body>
<center>
 <h2>我的留言板</h2>
<a href = "index.php">添加留言</a>
<a href = "show.php" >查看留言</a>
<hr width = "90%">
<h3>添加留言</h3>
<form action = "doAdd.php" method = "post">
 <table width = "380" border = "0" cellpadding = "4">
      <tr>
             <td  align = "right">标题:</td>
             <td><input type = "text" name = "title"></td>
     </tr>
      <tr>
             <td  align = "right">留言者:</td>
             <td><input type = "text" name = "author"></td>
     </tr>
     <tr>
             <td align = "right" valign = "top">留言内容:</td>
             <td><textarea name = "content" row = "5" cols = "30"></textarea></td>
     </tr>
      <tr>
              
	     <td colspan = "2" align = "center"><input type = "submit" value = "提交">
                               &#160;&#160;&#160; <input type = "reset" value = "重置"></td>
     </tr></table>
</form>
</center>

</body>
</html>


登录后复制
doAdd.php 添加留言部分

立即学习PHP免费学习笔记(深入)”;

<html>
<head>
<title>我的留言板</title>
</head>
<body>
<center>
 <h2>我的留言板</h2>
<a href = "index.php">添加留言</a>
<a href = "show.php" >查看留言</a>
<hr width = "90%">
<h3>添加留言</h3>
 <?php
    //执行留言信息添加操作
//1.获取要添加的留言信息,并补上其他辅助信息(ip地址、添加时间)
$title = $_POST["title"];
$author = $_POST["author"];
$content = $_POST["content"];
$ip = $_SERVER["REMOTE_ADDR"];
$addtime = time();
//2.拼装留言信息
$ly = "{$title}##{$author}##{$content}##{$ip}##{$addtime}@@@";
//echo $ly;
//3. 将留言添加到liuyan.txt文件中
$info = file_get_contents("liuyan.txt");
file_put_contents("liuyan.txt",$info.$ly);
echo "</br>";
//file_put_contents("liuyan.txt",$ly); 直接输出会覆盖上一条留言!
//4.输出留言成功!
echo "留言成功!";

 ?>
</center>

</body>
</html>

登录后复制

show.php 留言显示部分:

立即学习PHP免费学习笔记(深入)”;

<html>
<head>
<title>我的留言板</title>
<script>
function dodel(id){
  if(confirm("确定要删除么?"))
  {
    window.location ='del.php?id='+id; 
  }
}
</script>
</head>
<body>
<center>
 <h2>我的留言板</h2>
<a href = "index.php">添加留言</a>
<a href = "show.php" >查看留言</a>
<hr width = "90%">
<h3>查看留言</h3>
 <table border = "1" width = "700" >
     <tr>
	   <th>留言标题</th>
           <th>留言人</th>
           <th>留言内容</th>
	   <th>IP地址</th>
 <th>留言时间</th>
 <th>操作</th>
    </tr>
<?php
  // 获取留言信息,解析后输出到表格中
// 1.从留言liuyan.txt中获取留言信息
  $info = file_get_contents("liuyan.txt");
  // 2.去除留言内容最后的三个@@@符号
  $info = rtrim($info,"@");
  if(strlen($info)>=8){
  // 3.以@@@符号拆分留言信息为一条一条的(将留言信息以@@@符号拆分成留言数组)
  $lylist = explode("@@@",$info);
   
  // 4.遍历留言信息数组,对每条留言做再次解析;
  foreach($lylist as $k=>$v){
   $ly = explode("##",$v);
   echo "<tr>";
   echo "<td>{$ly[0]}</td>";
   echo "<td>{$ly[1]}</td>";
   echo "<td>{$ly[2]}</td>";
   echo "<td>{$ly[3]}</td>";
   echo "<td>".date("Y-m-d H:i:s",$ly[4])."</td>";
     echo "<td><a href = 'javascript:dodel({$k})'>删除</a></td>";
 
  }
  }
   
?>
</center>

</body>
</html>

登录后复制

del.php 留言删除部分:

立即学习PHP免费学习笔记(深入)”;

<html>
<head>
<title>我的留言板</title>
</head>
<body>
<center>
 <h2>我的留言板</h2>
<a href = "index.php">添加留言</a>
<a href = "show.php" >查看留言</a>
<hr width = "90%">
<h3>删除留言</h3>
 
<?php
//1. 获取要删除留言号
$id = $_GET["id"];
// 2.从留言liuyan.txt中获取留言信息
  $info = file_get_contents("liuyan.txt");
 
  //3.(将留言信息以@@@符号拆分成留言数组)
  $lylist = explode("@@@",$info);
  //4.使用unset删除指定的id留言
  unset($lylist[$id]);
  //还原留言信息为字串,并写回留言文件
  $newinfo = implode("@@@",$lylist);
  file_put_contents("liuyan.txt",$newinfo);
  echo "删除成功!";

 
   
?>
</center>

</body>
</html>
登录后复制


相关标签:
php
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号