真的写程序不容易,参看着书写也是觉得有些难度,而且非常辛苦。全部页面都做好了,一起学习的小伙伴可以一起看看。
post.php
<?php
header("Content-Type: text/html; charset=gb2312");
if(!isset($_GET['id'])){
echo '请求错误';
exit;
}
$path = substr($_GET['id'], 0,6); //获取目录,目录是“201212”是6个字符,所以截取0到6就是刚好是文件名
$entry = substr($_GET['id'], 7,9); //获取文件名(不带TXT),文件名是“06-151825”,所以截取7到9个字符,就是文件名
$file_name = 'contents/'.$path.'/'.$entry.'.txt'; //拼接出来前面完整的文件路径和文件名
if(file_exists($file_name)){
$fp = fopen($file_name, 'r');
if($fp){
flock($fp, LOCK_SH);
$result = fread($fp, filesize($file_name));
}
flock($fp, LOCK_UN);
fclose($fp);
}
$contents = explode('|', $result);
?>
<!doctype html>
<html>
<head>
<meta charset="gb2312">
<title><?php echo $contents['0'];?> - 风之翼灵的博客</title>
<!-- 新 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- 可选的Bootstrap主题文件(一般不用引入) -->
<link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<!-- 自定义CSS文件 -->
<link rel="stylesheet" href="./css/blog.css">
<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="//cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<div class="blog-masthead">
<div class="container">
<nav class="blog-nav">
<a class="blog-nav-item active" href="index.php">Home</a>
<a class="blog-nav-item" href="#">New features</a>
<a class="blog-nav-item" href="#">Press</a>
<a class="blog-nav-item" href="#">New hires</a>
<a class="blog-nav-item" href="#">About</a>
</nav>
</div>
</div>
<div class="container">
<div class="blog-header">
<h1 class="blog-title">风之翼灵'Blog</h1>
<p class="lead blog-description">这里记录着我的过去和将来</p>
</div>
<div class="row">
<div class="col-sm-8 blog-main">
<div class="blog-post">
<h2 class="blog-post-title"><?php echo $contents['0'];?></h2>
<p class="blog-post-meta"><?php echo $contents['2'];?></a></p>
<p><?php echo $contents['1'];?></p>
</div><!-- /.blog-post --><!-- /.blog-post --><!-- /.blog-post -->
<nav>
<ul class="pager">
<li><a href="./add.php">在发布一篇</a></li>
</ul>
</nav>
</div><!-- /.blog-main -->
<div class="col-sm-3 col-sm-offset-1 blog-sidebar">
<div class="sidebar-module sidebar-module-inset">
<h4>About</h4>
<p>风之翼灵,屌丝一枚...2015年学习PHP,一直在成长ING...</p>
</div>
<div class="sidebar-module">
<h4>Elsewhere</h4>
<ol class="list-unstyled">
<li><a href="#">GitHub</a></li>
<li><a href="#">Twitter</a></li>
<li><a href="#">Facebook</a></li>
</ol>
</div>
</div><!-- /.blog-sidebar -->
</div><!-- /.row -->
</div><!-- /.container -->
<footer class="blog-footer">
<p>fungj.com<a href="http://fungj.com">Bootstrap</a> by <a href="">FJ</a>.</p>
<p>
<a href="#">Back to top</a>
</p>
</footer>
</body>
</html>
add.php 添加文章
<?php
header("Content-Type: text/html; charset=gb2312");
$ok = true;
if (@$_POST['text'] != "" and $_POST['contents'] != ""){ //如果提交的内容有效
/* 设置相关提交的信息,并组合成一个字符串 */
$title = trim($_POST['text']); //设置标题,用trim删除左右两边的空白
$contents = trim($_POST['contents']); //设置内容,用trim删除左右两边的空白
$date = date("Y-m-d H:i:s",time()); //设置时间,用mktime()函数格式化时间
$blog_str = $title.'|'.$contents.'|'.$date; //将标题,内容,时间按照设计的组合起来成字符串
/* 设置文件存储规则 */
$ym = date("Ym",time()); //设置年月
$d = date('d',time()); //设置日
$time = date('His',time());//设置时间
$folder = 'contents/'.date("Ym",time()); //按照年月生成目录
$filename = date('d',time()).'-'.$time.'.txt'; //按照日,时间生成文件
$entry = date("Ym",time()).'-'.date('d',time()).'-'.date('His',time());
if (file_exists($folder) == false){
if (!mkdir($folder)){ //如果文件不存在,则创建文件夹
echo $folder."创建失败,请检查!"; //如果错误就提示
}
}
$fp =fopen($folder.'/'.$filename, 'w'); //打开存储的文件
if ($fp){
flock($fp, LOCK_EX); //锁定文件
$result = fwrite($fp, $blog_str); //打开文件,写入$blog_str里面的内容
flock($fp, LOCK_UN); //完成后释放锁定
fclose($fp); //关闭打开的文件
}
if (strlen($result) > 0){
$msg = '文章添加成功,<a href="./post.php?id='.$entry.'">查看发布的文章</a>';
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="gb2312">
<title>发布文章 - 风之翼灵的博客</title>
<!-- 新 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- 可选的Bootstrap主题文件(一般不用引入) -->
<link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<!-- 自定义CSS文件 -->
<link rel="stylesheet" href="./css/blog.css">
<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="//cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<div class="blog-masthead">
<div class="container">
<nav class="blog-nav">
<a class="blog-nav-item active" href="index.php">Home</a>
<a class="blog-nav-item" href="#">New features</a>
<a class="blog-nav-item" href="#">Press</a>
<a class="blog-nav-item" href="#">New hires</a>
<a class="blog-nav-item" href="#">About</a>
</nav>
</div>
</div>
<div class="container">
<div class="blog-header">
<h1 class="blog-title">风之翼灵'Blog</h1>
<p class="lead blog-description">这里记录着我的过去和将来</p>
</div>
<div class="row">
<div class="col-sm-8 blog-main">
<div class="blog-post">
<h2 class="blog-post-title">发布文章</h2>
<p class="blog-post-meta">心情、学习心得随意写^.^</p>
<hr>
<p><form action="add.php" method="post" name="form">
<table width="100%" border="0">
<tr>
<td>博客标题:<input name="text" type="text" class="form-control" size="80%"></td>
</tr>
<tr>
<td>时间:<?php echo date("Y-m-d H:i:s",time()); ?></td>
</tr>
<tr>
<td>内容:<textarea name="contents" class="form-control" cols="80%" rows="10"></textarea></td>
</tr>
<tr>
<td><br><input name="" type="submit" class="btn btn-default" value="提交"> <input name="" type="reset" class="btn btn-default" value="重置"></td>
</tr>
</table>
</form>
<?php echo @$msg; ?></p>
</div>
</div><!-- /.blog-main -->
<div class="col-sm-3 col-sm-offset-1 blog-sidebar">
<div class="sidebar-module sidebar-module-inset">
<h4>About</h4>
<p>风之翼灵,屌丝一枚...2015年学习PHP,一直在成长ING...</p>
</div>
<div class="sidebar-module">
<h4>Archives</h4>
<ol class="list-unstyled">
<li><a href="#">March 2014</a></li>
<li><a href="#">February 2014</a></li>
<li><a href="#">January 2014</a></li>
<li><a href="#">December 2013</a></li>
<li><a href="#">November 2013</a></li>
<li><a href="#">October 2013</a></li>
<li><a href="#">September 2013</a></li>
<li><a href="#">August 2013</a></li>
<li><a href="#">July 2013</a></li>
<li><a href="#">June 2013</a></li>
<li><a href="#">May 2013</a></li>
<li><a href="#">April 2013</a></li>
</ol>
</div>
<div class="sidebar-module">
<h4>Elsewhere</h4>
<ol class="list-unstyled">
<li><a href="#">GitHub</a></li>
<li><a href="#">Twitter</a></li>
<li><a href="#">Facebook</a></li>
</ol>
</div>
</div><!-- /.blog-sidebar -->
</div><!-- /.row -->
</div><!-- /.container -->
<footer class="blog-footer">
<p>fungj.com<a href="http://fungj.com">Bootstrap</a> by <a href="">FJ</a>.</p>
<p>
<a href="#">Back to top</a>
</p>
</footer>
</body>
</html>
login.php 登录页
<?php
include 'config/adminifo.php'; //调用配置文件
session_start(); //打开session
if (isset($_POST['username']) or isset($_POST['passwd'])){ //判断提交的用户名和密码不为空
$username = $_POST['username'];
$passwd = md5($_POST['passwd']); //设置用户名和密码两个变量,密码用md5加密
if ($username != $AUTH['user'] or $passwd != $AUTH['passwd']){
echo "用户名或密码错误!"; //如果提交的用户名和密码不等于admininfo.php里预设的话,提示错误
}else {
$_SESSION['user'] = $username; //如果都正确,那么设置SESSION
header("location:index.php"); //并且跳转会index.php页面
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="gb2312">
<title>用户登录 - 风之翼灵的博客</title>
<!-- 新 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- 可选的Bootstrap主题文件(一般不用引入) -->
<link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<!-- 自定义CSS文件 -->
<link rel="stylesheet" href="./css/blog.css">
<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="//cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<div class="blog-masthead">
<div class="container">
<nav class="blog-nav">
<a class="blog-nav-item active" href="index.php">Home</a>
<a class="blog-nav-item" href="#">New features</a>
<a class="blog-nav-item" href="#">Press</a>
<a class="blog-nav-item" href="#">New hires</a>
<a class="blog-nav-item" href="#">About</a>
</nav>
</div>
</div>
<div class="container">
<div class="blog-header">
<h1 class="blog-title">风之翼灵'Blog</h1>
<p class="lead blog-description">这里记录着我的过去和将来</p>
</div>
<div class="row">
<div class="col-sm-8 blog-main">
<div class="blog-post">
<h2 class="blog-post-title">用户登录</h2>
<p class="blog-post-meta">登录信息,获取相应管理权力...</p>
<hr>
<p><form action="login.php" method="post" name="form">
<table width="100%" border="0">
<tr>
<td>用户名:<input name="username" class="form-control" type="text" size="80%"></td>
</tr>
<tr>
<td>密码:<input name="passwd" class="form-control" type="password" size="80%"></td>
</tr>
<tr>
<td><br><input name="" class="btn btn-default" type="submit" value="提交"> <input name="" class="btn btn-default" type="reset" value="重置"></td>
</tr>
</table>
</form>
<?php echo @$msg; ?></p>
</div>
</div><!-- /.blog-main -->
<div class="col-sm-3 col-sm-offset-1 blog-sidebar">
<div class="sidebar-module sidebar-module-inset">
<h4>About</h4>
<p>风之翼灵,屌丝一枚...2015年学习PHP,一直在成长ING...</p>
</div>
<div class="sidebar-module">
<h4>Archives</h4>
<ol class="list-unstyled">
<li><a href="#">March 2014</a></li>
<li><a href="#">February 2014</a></li>
<li><a href="#">January 2014</a></li>
<li><a href="#">December 2013</a></li>
<li><a href="#">November 2013</a></li>
<li><a href="#">October 2013</a></li>
<li><a href="#">September 2013</a></li>
<li><a href="#">August 2013</a></li>
<li><a href="#">July 2013</a></li>
<li><a href="#">June 2013</a></li>
<li><a href="#">May 2013</a></li>
<li><a href="#">April 2013</a></li>
</ol>
</div>
<div class="sidebar-module">
<h4>Elsewhere</h4>
<ol class="list-unstyled">
<li><a href="#">GitHub</a></li>
<li><a href="#">Twitter</a></li>
<li><a href="#">Facebook</a></li>
</ol>
</div>
</div><!-- /.blog-sidebar -->
</div><!-- /.row -->
</div><!-- /.container -->
<footer class="blog-footer">
<p>fungj.com<a href="http://fungj.com">Bootstrap</a> by <a href="">FJ</a>.</p>
<p>
<a href="#">Back to top</a>
</p>
</footer>
</body>
</html>
edit.php 编辑文章
<?php
$login = false;
session_start(); //打开SESSION
if(!empty($_SESSION['user']) && $_SESSION['user']=='admin') //判断用户是否登录
$login = true;
$ok = false;
if (isset($_GET['entry'])){ //检测这这个值是否有效
echo "请求参数错误!";
exit();
}
if (empty($_SESSION['user']) || $_SESSION['user'] != 'admin'){
echo "<div> 你还没有登录,请返回从新登录<a href='login.php'>点击返回登录</a></div>";
exit();
}
@$path = substr($_GET['id'], 0,6); //文章存储的目录
@$entry = substr($_GET['id'], 7,9); //日志文件名称
$file_name = 'contents/'.$path.'/'.$entry.'.txt'; //拼接出完整的文件地址
if (file_exists($file_name)){ //判断文件是否存在
$fp = fopen($file_name, 'r'); //已读取的模式打开文件
if ($fp){ //判读文件是否打开成功
flock($fp, LOCK_SH); //锁定文件
$result = fread($fp, filesize($file_name)); //读取文件
}
flock($fp, LOCK_UN); //解除锁定
fclose($fp); //关闭文件
$contents_arr = explode('|', $result); //把文件内容分开,存入数组
}
if (@$_POST['edittext'] and @$_POST['editcontents'] != null){ //判断用户提交的两个东东都不是空
$title = trim($_POST['edittext']); //设置标题为用户提交的...并用trim去除两边的空格
$contents = trim($_POST['editcontents']); //和上面同理
$date = date("Y-m-d H:i:s",time());
if (file_exists($file_name)){ //判断文件是否存在
$blog_temp = str_replace($contents_arr['0'], $title, $result); //替换标题
$blog_str = str_replace($contents_arr['1'], $contents,$blog_temp); //内容
$blog_date = str_replace($contents_arr['2'], $date); //时间
$fp = fopen($file_name, 'w'); //已写入的模式打开文件
if ($fp){
flock($fp, LOCK_EX); //锁定文件
$result = fwrite($fp,$blog_str); //写入文件
flock($fp, LOCK_UN); //解除锁定
fclose($fp); //关闭文件
}
}
if (strlen($result)>0){ //判断修改是否成功
$ok = true;
$msg = '修改成功,<a href="post.php?id='.$_GET['id'].'">查看修改后的文章</a>';
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="gb2312">
<title>编辑文章 - 风之翼灵的博客</title>
<!-- 新 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- 可选的Bootstrap主题文件(一般不用引入) -->
<link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<!-- 自定义CSS文件 -->
<link rel="stylesheet" href="./css/blog.css">
<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="//cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<div class="blog-masthead">
<div class="container">
<nav class="blog-nav">
<a class="blog-nav-item active" href="index.php">Home</a>
<a class="blog-nav-item" href="#">New features</a>
<a class="blog-nav-item" href="#">Press</a>
<a class="blog-nav-item" href="#">New hires</a>
<a class="blog-nav-item" href="#">About</a>
</nav>
</div>
</div>
<div class="container">
<div class="blog-header">
<h1 class="blog-title">风之翼灵'Blog</h1>
<p class="lead blog-description">这里记录着我的过去和将来</p>
</div>
<div class="row">
<div class="col-sm-8 blog-main">
<div class="blog-post">
<h2 class="blog-post-title">编辑文章</h2>
<p class="blog-post-meta">文章写错了没关系,改改总可以的...</p>
<hr>
<p>
<?php
if ($ok == false){
?>
<form action="edit.php?id=<?php echo $_GET['id']; ?>" method="post" name="form">
<table width="100%" border="0">
<tr>
<td>博客标题:<input name="edittext" class="form-control" type="text" size="80%" value = "<?php echo $contents_arr['0']; ?>"></td>
</tr>
<tr>
<td>上次修改时间:<?php echo $contents_arr['2']; ?></td>
</tr>
<tr>
<td>内容:<textarea name="editcontents" class="form-control" cols="80%" rows="10"><?php echo $contents_arr['1']; ?></textarea></td>
</tr>
<tr>
<td><br><input name="" class="btn btn-default" type="submit" value="提交"> <input name="" class="btn btn-default" type="reset" value="重置"></td>
</tr>
</table>
<?php } ?>
</form>
<?php echo @$msg; ?></p>
</div>
</div><!-- /.blog-main -->
<div class="col-sm-3 col-sm-offset-1 blog-sidebar">
<div class="sidebar-module sidebar-module-inset">
<h4>About</h4>
<p>风之翼灵,屌丝一枚...2015年学习PHP,一直在成长ING...</p>
</div>
<div class="sidebar-module">
<h4>Elsewhere</h4>
<ol class="list-unstyled">
<li><a href="#">GitHub</a></li>
<li><a href="#">Twitter</a></li>
<li><a href="#">Facebook</a></li>
</ol>
</div>
<div class="sidebar-module sidebar-module-inset">
<h4>Login</h4>
<p><?php
if ($login){ //判断是否登录
echo '<a href="logout.php">退出 </a>';
}else {
echo '<a href="login.php">登录 </a>';
}
?></p>
</div>
</div><!-- /.blog-sidebar -->
</div><!-- /.row -->
</div><!-- /.container -->
<footer class="blog-footer">
<p>fungj.com<a href="http://fungj.com">Bootstrap</a> by <a href="">FJ</a>.</p>
<p>
<a href="#">Back to top</a>
</p>
</footer>
</body>
</html>
delete.php 文章删除
<?php
session_start(); //SESSION开始
if(!empty($_SESSION['user']) && $_SESSION['user']=='admin') //判断用户是否登录
$login = true;
$ok = false;
if (empty($_SESSION['user']) or $_SESSION['user'] != 'admin'){
echo "<div> 你还没有登录,请返回从新登录<a href='login.php'>点击返回登录</a></div>"; //如果没有登录或者用户名错误
exit;
}
if (!isset($_GET['id'])){
if (!isset($_POST['id'])){ //判断$_GET['id'])是否设置了
$ok == true;
$msg = '参数错误,<a href="index.php">返回首页</a>'; //没有就返回首页
}else{
//下面是删除操作
$path = substr($_POST['id'], 0,6); //文章存储的目录
$entry = substr($_POST['id'], 7,9); //文章存储的文件名称
$file_name = 'contents/'.$path.'/'.$entry.'.txt'; //拼接出完整的文件地址
if (@unlink($file_name)){ //unlink 删除文件
$ok = true;
echo '文章删除成功。<a href="index.php">返回首页</a>';
}else {
$ok = false;
echo '文章删除失败!。<a href="index.php">返回首页</a>';
}
}
}else {
$form_data = '';
$path = substr($_GET['id'], 0,6); //文章存储的目录
$entry = substr($_GET['id'], 7,9); //文章存储的文件名称
$file_name = 'contents/'.$path.'/'.$entry.'.txt'; //拼接出完整的文件地址
if (file_exists($file_name)){ //判断文件是否存在
$form_data = '<input type="hidden" name="id" value="'.$_GET['id'].'">';
}else {
$ok = true;
$msg = '要删除的文章不存在!<a href="index.php">返回首页</a>';
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="gb2312">
<title>删除文章 - 风之翼灵的博客</title>
<!-- 新 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- 可选的Bootstrap主题文件(一般不用引入) -->
<link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<!-- 自定义CSS文件 -->
<link rel="stylesheet" href="./css/blog.css">
<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="//cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<div class="blog-masthead">
<div class="container">
<nav class="blog-nav">
<a class="blog-nav-item active" href="index.php">Home</a>
<a class="blog-nav-item" href="#">New features</a>
<a class="blog-nav-item" href="#">Press</a>
<a class="blog-nav-item" href="#">New hires</a>
<a class="blog-nav-item" href="#">About</a>
</nav>
</div>
</div>
<div class="container">
<div class="blog-header">
<h1 class="blog-title">风之翼灵'Blog</h1>
<p class="lead blog-description">这里记录着我的过去和将来</p>
</div>
<div class="row">
<div class="col-sm-8 blog-main">
<div class="blog-post">
<h2 class="blog-post-title">删除文章</h2>
<p class="blog-post-meta">再见了.........</p>
<hr>
<?php
if ($ok == false){
?>
<form action="delete.php" method="post">
<div style="color:#F00">是否确定要删除?删除后无法恢复!</div>
<input name="" class="btn btn-default" type="submit" value="确定"></input>
<?php echo $form_data; ?>
</form>
<?php
}
?>
<?php
if ($ok == true){
echo $msg;
}
?>
</div>
</div><!-- /.blog-main -->
<div class="col-sm-3 col-sm-offset-1 blog-sidebar">
<div class="sidebar-module sidebar-module-inset">
<h4>About</h4>
<p>风之翼灵,屌丝一枚...2015年学习PHP,一直在成长ING...</p>
</div>
<div class="sidebar-module">
<h4>Elsewhere</h4>
<ol class="list-unstyled">
<li><a href="#">GitHub</a></li>
<li><a href="#">Twitter</a></li>
<li><a href="#">Facebook</a></li>
</ol>
</div>
<div class="sidebar-module sidebar-module-inset">
<h4>Login</h4>
<p><?php
if ($login){ //判断是否登录
echo '<a href="login.php">退出 </a>';
}else {
echo '<a href="login.php">登录 </a>';
}
?></p>
</div>
</div><!-- /.blog-sidebar -->
</div><!-- /.row -->
</div><!-- /.container -->
<footer class="blog-footer">
<p>fungj.com<a href="http://fungj.com">Bootstrap</a> by <a href="">FJ</a>.</p>
<p>
<a href="#">Back to top</a>
</p>
</footer>
</body>
</html>
archives.php 文章归档
<?php
$ok = false;
if (!isset($_GET['ym']) and empty($_GET['ym'])){ //判断请求的参数是否正确
$ok = true;
$msg = '参数错误,<a href="index.php">返回首页</a>';
}
$folder_array = array(); //做一个数组
$dir = 'contents'; //目录
$folder = $_GET['ym'];
$file_dir = $dir.'/'.$folder; //拼接目录
if (!is_dir($dir.'/'.$folder)){
$ok = true;
$msg = '参数错误,<a href="index.php">返回首页</a>';
}
$dh = opendir($dir); //打开目录
if ($dh){
$filename = readdir($dh); //读取目录里的文件
while ($filename){ //循环输出文件
if ($filename != '.' and $filename != '..'){ //判断文件名是否有效
$file_name = $filename;
array_push($folder_array, $file_name); //把$file_name里的内容压入$folder_array里面
}
$filename = readdir($dh);
}
}
rsort($folder_array); //对目录进行排序
$post_data = array();
$dh = opendir($file_dir);
while (($filename = readdir($dh)) !==false ){
if (is_file($file_dir.'/'.$filename)){
$file = $filename;
$file_name = $dir.'/'.$folder.'/'.$file;
if (file_exists($file_name)){ //判断文件是否存在
$fp = fopen($file_name, 'r'); //只读方式打开文件
if ($fp){ //判断文件是否打开成功
flock($fp, LOCK_SH); //锁定文件
$result = fread($fp, filesize($file_name)); //读取文件
}
flock($fp, LOCK_UN); //解锁
fclose($fp); //关闭文件
}
$temp_data = array();
$contents_array = explode('|', $result); //分割成数组
//以下读取文件中的三个信息
$temp_data['SUBJECT'] = $contents_array['0']; //标题
$temp_data['DATE'] = $contents_array['2']; //时间
$temp_data['CONTENTS'] = $contents_array['1']; //内容
array_push($post_data, $temp_data);
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="gb2312">
<title>风之翼灵的博客</title>
<!-- 新 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- 可选的Bootstrap主题文件(一般不用引入) -->
<link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<!-- 自定义CSS文件 -->
<link rel="stylesheet" href="./css/blog.css">
<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="//cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<div class="blog-masthead">
<div class="container">
<nav class="blog-nav">
<a class="blog-nav-item active" href="index.php">Home</a>
<a class="blog-nav-item" href="#">New features</a>
<a class="blog-nav-item" href="#">Press</a>
<a class="blog-nav-item" href="#">New hires</a>
<a class="blog-nav-item" href="#">About</a>
</nav>
</div>
</div>
<div class="container">
<div class="blog-header">
<h1 class="blog-title">文章归档 - 风之翼灵'Blog</h1>
<p class="lead blog-description">这里记录着我的过去和将来</p>
</div>
<div class="row">
<div class="col-sm-8 blog-main">
<div class="blog-post">
<h2 class="blog-post-title"></h2>
<p class="blog-post-meta">
</p>
<?php
if ($ok = false){
foreach ($post_data as $post){
?>
<p><?php echo $post['SUBJECT']; ?></p>
<p><?php echo $post['DATE']; ?></p>
<p><?php echo $post['CONTENTS']; ?></p>
</div>
<?php
}
}else {
echo $msg;
}
?>
<!-- /.blog-post --><!-- /.blog-post --><!-- /.blog-post -->
</div><!-- /.blog-main -->
<div class="col-sm-3 col-sm-offset-1 blog-sidebar">
<div class="sidebar-module sidebar-module-inset">
<h4>About</h4>
<p>风之翼灵,屌丝一枚...2015年学习PHP,一直在成长ING...</p>
</div>
<div class="sidebar-module">
<h4>Elsewhere</h4>
<ol class="list-unstyled">
<li><a href="#">GitHub</a></li>
<li><a href="#">Twitter</a></li>
<li><a href="#">Facebook</a></li>
</ol>
</div>
</div><!-- /.blog-sidebar -->
</div><!-- /.row -->
</div><!-- /.container -->
<footer class="blog-footer">
<p>fungj.com<a href="http://fungj.com">Bootstrap</a> by <a href="">FJ</a>.</p>
<p>
<a href="#">Back to top</a>
</p>
</footer>
</body>
</html>
<?php closedir($dh); //别忘记关闭打开的目录
?>
logout.php 退出
<?php
session_start(); //SESSION开始
$info = '';
if (isset($_SESSION['suer'])){
$_SESSION['user'] = '';
$msg = '成功退出,再见!<a href="index.php">返回首页</a>';
}else {
$msg = '您未曾登录或者已经登录超时!<a href="index.php">返回首页</a>';
}
?>
<!doctype html>
<html>
<head>
<meta charset="gb2312">
<title>退出</title>
</head>
<body>
<div align="center"><?php echo $msg; ?></div>
</body>
</html>