首页程序: <?php $login = false; session_start(); if(!empty($_SESSION['user']) && $_SESSION['user'] == 'admin') //判断用户是否登录 $login = true; $file_array = array(); $folder_array = array(); $dir = 'contents'; $dh = opendir($dir); //打开保存日志的目录 if($dh) { $filename = readdir($dh); //读取目录下的文件 while($filename) //循环处理按年月归档的日志文章 { if($filename !='.' && $filename !='..') { $folder_name = $filename; array_push($folder_array,$folder_name); } $filename = readdir($dh); } } rsort($folder_array); //对目录排序 $post_data = array(); foreach($folder_array as $folder) { $dh = opendir($dir.'/'.$folder); //处理每个目录下的日志文件 while(($filename = readdir($dh)) !== FALSE) { if(is_file($dir.'/'.$folder.'/'.$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(); $content_array = explode('|',$result); $temp_data['SUBJECT'] = $content_array[0]; //文章标题 $temp_data['DATE'] = date('Y-m-d H:i:s',$content_array[1]); //发表时间 $temp_data['CONTENT'] = $content_array[2]; //文章内容 $file = substr($file,0,9); //日志文章所在文件夹名 $temp_data['FILENAME'] = $folder.'-'.$file; array_push($post_data,$temp_data); } } } ?> <html> <head> <title>基于文本的简易BLOG</title> <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body> <div id="container"> <div id="header"> <h1>我的BLOG</h1> </div> <div id="title"> ---- I have dream.... </div> <div id="left"> <?php foreach($post_data as $post) //显示所有日志文章 { ?> <div id="blog_entry"> <div id="blog_title"><?php echo $post['SUBJECT'];?></div> <div id="blog_body"> <div id="blog_date"><?php echo $post['DATE'];?></div> <?php echo $post['CONTENT'];?> <div> <?php if($login) { echo '<a href="12-5.php?entry='.$post['FILENAME'].'">编辑</a> <a href="12-6.php?entry='.$post['FILENAME'].'">删除</a>'; //输出日志文章的“编辑”和“删除”连接 } ?> </div> </div> </div> <?php } ?> </div> <div id="right"> <div id="sidebar"> <div id="menu_title">关于我</div> <div id="menu_body"> 我是个PHP爱好者 <br/><br/> <?php if($login) {echo '<a href="12-8.php">退出</a>';} else{ echo'<a href="12-3.php">登录</a>';}?> </div> </div> <br/> <div id="sidebar"> <div id="menu_title">日志归档</div> <?php foreach($folder_array as $ym) //输出日志按年月的归纳 { $entry = $ym; $ym = substr($ym,0,4).'-'.substr($ym,4,2); echo '<div id="menu_body"><a href="12-7.php?ym='.$entry.'">'.$ym.'</a></div>'; } ?> </div> </div> <div id="footer"> Copyright 2016 </div> </div> </body> </html>
转载于:https://my.oschina.net/null7654321/blog/712851