Abstract: 把新闻信息,用户信息存放在文件中,用对文件的读写,实现用户的登录注册以及对新闻的增删改查。
一、目录结构
###目录结构:
| index.php //初始化页面
|——– data //文件存放目录
| news.db //存放新闻信息文件
| users.db //存放用户信息文件
|——— lib //脚本存放目录
| login.html // 用户登录界面
| checklogin.php //处理用户登录信息
| logup.html //用户注册界面
| checkSignUp.php //处理用户注册信息
| addNews.php //添加新闻界面
| del.php //删除新闻界面
| upload.php //上传头像界面
| getpasswd.php //修改密码界面
| checkpass.php //对hgetpasswd.php发送的数据进行处理
|——– css //用户登录和注册界面的css目录
| login.css //用户登录的css
| logup.css //用户注册的css
|——– upload //用户上传头像目录
|——– basedir //存放页面头部尾部和一些公共函数
| header.php //页面头部
| footer.php //页面尾部
| function.php //定义了一些公共函数
二、新闻界面 index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| <?php session_start(); ?> <html> <head> <meta charset="UTF-8"> <title>news</title> </head> <body> <br> <!--区分普通用户和管理员用户--> <?php if(isset($_SESSION['uname'])){ echo <<<EOF <div> <img src="../images/logo.png" height="100px" width="100px" style="float: left"> </div> <div style="float: right"> <img src='../upload/headimg.jpg' height="60px" width="60px"> <br> <a href="../logout.php">注销用户</a> </div> EOF; } ?> <h1 align="center">ICQ新闻中心</h1> <hr> <table align="center"> <tr> <th>热度</th> <th>标题</th> <th>作者</th> <th>时间</th> </tr> <?php //用只读方式打开news.db文件 $fhandle = fopen("./data/news1.db", "r"); //设置一个计数器,小于3时,每个文章标题下面增加图片和简介 $top3Cnt=1; while(!feof($fhandle)) { $fline = fgets($fhandle); $data = explode('||', $fline); @$title = base64_decode($data[1]); @$content = base64_decode($data[2]); @$date = $data[3]; @$author = base64_decode($data[4]); @$hot = $data[5]; @$imgUrl = $data[6]; echo <<<EOF <tr align="center"> <td>$hot</td> <td><a href="myl.php">$title</a></td> <td>$author</td> <td>$date</td> </tr> EOF; //新闻前三个显示图片和简介 if($top3Cnt<=3){ $tmpContent = mb_substr(base64_decode($data[2]), 0,50); echo <<<EOF <tr> <td><img src="$imgUrl" height="80" width="80"></td> <td colspan='4'><b>内容简介:</b>$tmpContent</td> </tr> EOF; } $top3Cnt++; } ?> </table> </body> </html>
|
普通用户显示界面:
管理员用户显示界面:
三、用户登录和用户注册
1.login.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>登录页面</title> <link rel="stylesheet" type="text/css" href="css/login.css"> </head> <body> <div class="box"> <div class="top"> <h1>登录</h1> </div> <form action="checklogin.php" method="post"> <input type="text" name="username" placeholder="手机号/邮箱/用户名"> <br> <input type="password" name="password" placeholder="密码"> <br> <input class="input_code" type="text" name="verifycode" placeholder="验证码"> <img class="verifycode" src="https://passport.360.cn/captcha.php?m=create&app=i360&scene=login&userip=&level=default&sign=8820a4&r=1564540365&border=none&_=1564540365972"> <br> <a class="signup" href="logup.html">注册账号</a> <a class="getpass" href="./tmp/getpasswd.php">忘记密码</a> <br> <input class="login" type="submit" name="submit" value="登录"> </form> </div> </body> </html>
|
2.checklogin.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| <?php session_start(); ?> <html> <head> <title> 登录 </title> </head> <body> <?php $fhandle = fopen('./data/users.db', 'r'); while(!feof($fhandle)) { $InMes = fgets($fhandle); $mesArray = explode('||', $InMes); if(count($mesArray)!=2){ continue; } if(trim($mesArray[0])==$_POST['username'] && trim($mesArray[1])==$_POST['password']){ $_SESSION['uname'] = $_POST['username']; echo "正在进入后台"; echo <<<EOF <script>location.href="./data/addNews.php"</script> EOF; $flag=0; break; } $flag = 1; } if($flag==1){ echo "<script>alert('账号或密码错误!重新输入。')</script>"; echo "<script>history.back()</script>"; } ?> </div> </body> </html>
|
登录成功界面:
登录失败界面:
3. checklogup.php
logup.html
与login.html
程序接近
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <?php session_start(); $fhandle = fopen('./data/users.db', 'a+'); $userMes = array(); $storeMes = array(); @$userMes[0] = $_POST['username']; @$userMes[1]=$_POST['password']; @$userMes[2]=$_POST['checkpasswd']; if(strcmp($userMes[1], $userMes[2])!=0){ echo "<script>alert('两次输出密码不同!请重新输入!')</script>"; echo "<script>history.back()</script>"; } else { $storeMes[0] = $userMes[0]; $storeMes[1] = $userMes[1]; $inMes = implode('||', $storeMes); if (count($userMes) == 3) { fputs($fhandle, $inMes . "\n"); fclose($fhandle); echo "<script>alert('用户注册成功')</script>"; echo "<script>location.href='login.html'</script>"; $_SESSION['uname'] = $_POST['username']; } } ?>
|
用户注册失败:
用户注册成功:
四、新闻的增删改查
1. 添加新闻 addNews.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| <!--添加头部信息--> <?php include '../basedir/header.php'; include '../basedir/function.php';?> <div align="center"> <?php if($_SESSION['uname']==NULL) { echo "<script>alert('请登录'); location.href='../login.html'</script>"; die("请登录"); } ?> <a href="upload.php">上传头像</a>           <a href="del.php">删除新闻</a>           <a href="getpasswd.php">修改密码</a> </div> <br><br><br> <div align="center"> <form action="addNews.php" method="post"> <?php if($_SESSION['uname']==NULL) echo "<script>alert('请登录'); location.href='../login.html'</script>"; die("请登录"); ?> <input type="text" name="title" placeholder="文章标题"><br> <br> <input type="number" name="hot" placeholder="热度"><br> <br> <input type="text" name="imgUrl"><br> <br> <textarea name="content" cols="100" rows="50" placeholder="文章内容"></textarea><br> <input type="submit" name="submit" value="提交"> </form> </div> <?php $fhandle = fopen('../data/news2.db', 'a'); $t = time(); $arrMes = array(); @$arrMes[0] = computerId('../data/news2.db')+1; @$arrMes[1] = base64_encode($_POST['title']); @$arrMes[2] = base64_encode($_POST['content']); @$arrMes[3] = date("Y-m-d", $t); @$arrMes[4] = base64_encode($_SESSION["username"]); @$arrMes[5] = $_POST['hot']; @$arrMes[6] = $_POST['imgUrl']; if (count($arrMes) == 7) { $writeMes = implode('||', $arrMes); $fline = fputs($fhandle, $writeMes."\n"); if ($fline != flase) { echo "<script>alert(\"新闻添加成功!\")</script>"; echo "<a href='../index.php'>点击查看</a>"; } } fclose($fhandle); ?> <!--添加尾部信息--> <?php include "../basedir/footer.php";?>
|
添加新闻页面:
2. 删除新闻 del.php
思路:每条新闻对应一个id
号,通过输入id
判断要删除的新闻,程序循环读取文件每一行数据,再通过字符串函数把id
号取出,若id
不匹配把每行数据push
到一个数组里面,若相等则跳过。然后再通过for
循环把存入数据的数组以w
格式重新写入文件即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| <?php include '../basedir/header.php'; include '../basedir/function.php';?> <?php if($_SESSION['uname']==NULL) echo "<script>alert('请登录'); location.href='../login.html'</script>"; ?> <div align="center"> <a href="upload.php">上传头像</a>           <a href="../data/addNews.php">添加新闻</a>           <a href="getpasswd.php">修改密码</a> <br><br><br> <form action="del.php" method="post"> <input type="text" name="id" placeholder="请输入要删除文章的编号"> <input type="submit" name="submit" value="提交"> </form> <a href="../index.php" target="_blank">点击查看</a> </div> <table> <table align="center"> <tr> <th>标号</th> <th>标题</th> <th>作者</th> </tr> <?php $f = fopen('../data/news1.db', 'r'); while(!feof($f)){ $fline = fgets($f); $data = explode('||', $fline); @$artId = $data[0]; @$title = base64_decode($data[1]); @$author = base64_decode($data[4]); echo <<<EOF <tr align="center"> <td>$artId</td> <td>$title</td> <td>$author</td> </tr> EOF; } ?> </table>
<?php @$id = $_POST['id']; $fhandle = fopen('../data/news1.db', 'r'); $transArr = array(); while(!feof($fhandle)){ $fline = fgets($fhandle); $tmpLine = explode('||', $fline); if($tmpLine[0] == $id){ continue; } array_push($transArr, $tmpLine); } fclose($fhandle); for($i=0; $i<count($transArr); $i++) { echo "<br>"; } echo "<br>";
$fhandle = fopen('../data/news1.db', 'w'); for($i=0; $i<count($transArr); $i++) { if(count($transArr[$i])==7) { $transArr[$i][0] = $i + 1; $str = implode('||', $transArr[$i]); if($str=="\n"){ continue; } fputs($fhandle, $str); } } if(isset($_POST['id'])) { if (count($transArr) == computerId('../data/news1.db')) { echo "<script>alert('删除成功'); location.href='../tmp/del.php';</script>"; } } fclose($fhandle); ?> <?php include "../basedir/footer.php";?>
|
添加好新闻后,点击删除新闻可以进入删除新闻界面
3. 修改密码
getpasswd.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <?php include '../basedir/header.php'; include '../basedir/function.php';?> <div align="center"> <?php if($_SESSION['uname']==NULL) { echo "<script>alert('请登录'); location.href='../login.html'</script>"; die("请登录"); } ?> <a href="upload.php">上传头像</a>           <a href="../data/addNews.php">添加新闻</a>           <a href="del.php">删除新闻</a> <br><br><br> <form action="checkpass.php" method="post"> <label>输入旧密码:</label><input type="text" name="oldpasswd"> <label>输入新密码:</label><input type="text" name="newpasswd"> <label>确认密码:</label><input type="text" name="checknewpasswd"> <input type="submit" name="submit" value="提交"> </form> </div> <?php include "../basedir/footer.php";?>
|
checkpass.php
思路:先把每行数据读出存到一个数组里,用户名匹配成功后,先判断oldpass
是否正确,不正确则返回,重新输入,若匹配成功,再判断newpasswd
和checknewpasswd
是否一致,不一致返回,重新输入,一致的话,把该用户的密码改成新密码后入栈。最后处理完的数据写入文件中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| <?php session_start(); if($_SESSION['uname']==NULL) { echo "<script>alert('请登录'); location.href='../login.html'</script>"; die("请登录"); } if($_POST['oldpasswd']==NULL && $_POST['newpasswd']==NULL && $_POST['checknewpasswd']==NULL){ echo "<script>alert(\"密码不能为空\"); history.back();</script>"; } $tmparr = array(); $userPasswd = array(); $oldpass = $_POST['oldpasswd']; $newpass = $_POST['newpasswd']; $checknew = $_POST['checknewpasswd']; $fhandle = fopen('../data/user1.db', 'r'); while(!feof($fhandle)) { $line = fgets($fhandle); $lineArr = explode('||', $line); if ($lineArr[0] == $_SESSION['uname']) { $userPasswd[0] = $lineArr[0]; $userPasswd[1] = $newpass."\n"; $checkold = $lineArr[1]; array_push($tmparr, $userPasswd); continue; } array_push($tmparr, $lineArr); } fclose($fhandle); if(trim($checkold)!=$oldpass){ echo "<script>alert(\"密码不正确!\")</script>"; }else { if ($newpass == $checknew) { $fhandle = fopen('../data/user1.db', w); for ($i = 0; $i < count($tmparr); $i++) { $str = implode('||', $tmparr[$i]); echo $str; echo "<br>"; fputs($fhandle, $str); } fclose($fhandle); echo "<script>alert(\"密码修改成功,请重新登录!\")</script>"; //echo "<script>location.href='../logout.php'</script>";
}else{ echo "<script>alert(\"新密码不匹配,请重新输入!\")</script>"; } } ?>
|
修改密码界面:
密码修改后登出账号,重新登录。
五、上传头像 upload.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| <?php include '../basedir/function.php'; include '../basedir/header.php'; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>上传图片</title> </head> <body> <?php if($_SESSION['uname']==NULL) echo "<script>alert('请登录'); location.href='../login.html'</script>"; ?> <div align="center"> <a href="del.php">删除新闻</a>           <a href="../data/addNews.php">添加新闻</a>           <a href="getpasswd.php">修改密码</a> <br><br><br> <form action="upload.php" method="post" enctype="multipart/form-data"> <label>文件名:</label> <input type="file" name="file" ><br> <input type="text" name="title"><br> <input type="submit" name="submit" value="提交"> </form> </div> </body> </html> <?php if(isset($_POST['submit'])){ $name = $_FILES['file']['name']; $temp_name = $_FILES['file']['tmp_name']; if(isset($name)){ if(!empty($name)){ $location = "../upload/"; $path = $location . $name; if(move_uploaded_file($temp_name, $location.$name)){ $_SESSION['headImg'] = $path; echo 'File uploaded successfully'; echo <<<EOF <div align="center"> <img src=$path> </div> EOF; } } } else { echo 'You should select a file to upload !!'; } } ?> <?php include '../basedir/footer.php'?>
|
上传头像界面:
六、退出账号 logout.php
1 2 3 4 5
| <?php session_start(); session_destroy(); echo "<script>alert('账号已退出'); location.href='login.html'</script>" ?>
|
在任何一个界面点击注销账号均能成功退出,重新返回用户登录界面
点击确定,返回用户登录界面。
七、问题与总结
- 目前的代码有些冗余,比如删除新闻和修改密码,都是把需要的信息读取出来,然后稍作处理再写回文件,可以在
function.php
文件里写一个函数,需要时直接调用函数就行;删除新闻界面和初始化界面都有展示新闻的功能,也可可以通过一个函数来实现展示。
- 对写入文件的内容判断不严谨,会把
\n
写入,文件内容间有空行时,统计总的id
数会把空行也给统计进去,进而导致删除新闻时,不成功。把空行删完以后,才会执行成功。目前对文件操作\n
理解不深,正在寻找方法解决。
- 代码移植性差,需要修改地方较多。