index.php源码:
<html>
<head>
<title>Test Site</title>
<link href="Css/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="header">
<?php
session_start();
if (isset($_COOKIE['username'])) {
$_SESSION['username'] = $_COOKIE['username'];
$_SESSION['islogin'] = 1;
}
if (isset($_SESSION['islogin'])) {
echo "[<a href='logoff.html'>注销</a>][<a href='manage.php'>管理</a>]";
}else{
echo "[<a href='login.html'>登录</a>][<a href='register.html'>注册</a>]";
}
?>
</div>
<div class="body">
<hr>
<table width="1000" border="0" align="center">
<tbody>
<tr>
<th width="100">热度</th>
<th width="500">标题</th>
<th width="100">作者</th>
<th width="100">时间</th>
<th width="200">操作</th>
</tr>
<?php
$file = fopen('Data/news.db','r');
while(!feof($file)) {
$line = explode('|', fgets($file));
if($line[0]==''){continue;}
$article_image = base64_decode($line[1]);
$article_title = base64_decode($line[2]);
$article_content = base64_decode($line[3]);
$article_date = date("Y-m-d",$line[5]);
echo <<<EOF
<tr>
<td>{$line[0]}</td>
<td>{$article_title}</td>
<td>{$line[4]}</td>
<td>{$article_date}</td>
<td><button onclick=>赞</button>|<button onclick=>踩</button></td>
</tr>
<tr height='100'>
<td bgcolor="#cccccc"><img src="{$article_image}" width="100" height="100"></td>
<td bgcolor="#cccccc" colspan='4'>{$article_content}</td>
</tr>
EOF;
}
fclose($file);
?>
</tbody>
</table>
</div>
<footer class="footer">
©2019 ICQ 意见反馈 京ICP证03173号 京公网安备110000012931号
</footer>
</body>
</html>
登录界面login.html和login.php
login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>用户登录</title>
<link rel="stylesheet" type="text/css" href="Css/style.css">
</head>
<body>
<div class="box">
<div class="top">
<a href="#">登录</a>|<a href="register.html">注册</a>
</div>
<form action="login.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="getpass" href="#">忘记密码</a>
<br>
<input class="login" type="submit" name="login" value="登录">
</form>
</div>
</body>
</html>
login.css
/* 登陆注册页面样式 */
.box {
height: 500px;
width: 40%;
background: white;
margin: 150px 20% 0 30% ;
text-align: center;
}
.top {
padding-top: 20px;
}
input {
height: 43px;
width: 355px;
margin: 10px 0;
font-size: 17px;
}
.input_code {
width: 240px;
margin-left: 0px;
}
.verifycode {
margin-left: 10px;
height: 43px;
width: 100px;
}
.getpass {
margin-top: 50px;
margin-left: 50%;
}
.login {
line-height: 40px;
font-size: 14px;
}
/* 主页页面样式 */
table {
border-collapse: collapse;
}
caption {
font-size: larger;
margin: 1em auto;
}
th,td {
padding: .65em;
}
th,td {
border-bottom: 1px solid #ddd;
border-top: 1px solid #ddd;
text-align: center;
}
button {
background-color: Transparent;
background-repeat: no-repeat;
border: none;
cursor: pointer;
overflow: hidden;
}
.header {
text-align: right;
}
.body {
text-align: center;
}
.footer {
top: auto;
position: absolute;
bottom: 0px;
text-align: center;
}
longin.php
<?php
session_start();
if(isset($_POST['login'])){
$username = trim($_POST['username']);
$password = trim($_POST['password']);
$file_users = fopen("Data/users.db",'r');
while(!feof($file_users)){
$_logins = explode("|",fgets($file_users));
if (base64_encode($username) == $_logins[0] && base64_encode($password) == $_logins[1]) {
$_SESSION['username']=$username;
$_SESSION['islogin']=1;
echo "<script>alert('登陆成功!');location.href='index.php';</script>";
}
}
echo "<script>alert('账号密码错误!');location.href='login.html';</script>";
}
?>
登出logout.php
<?php
session_start();
$username = $_SESSION['username'];
$_SESSION = array();
session_destroy();
setcookie('username', '', time()-99);
echo "下次不要在来了, ".$username.'<br>';
echo "<a href='login.html'>重新登录</a>";
?>
注册
regist.php
<?php
if(isset($_POST['register'])){
$username = trim($_POST['username']);
$password = trim($_POST['password']);
$file_users = fopen("Data/users.db",'a+');
while(!feof($file_users)){
$_logins = explode("|",fgets($file_users));
if($_logins[0]==base64_encode($username)){
echo "<script>alert('该用户已被注册!');location.href='register.html';</script>";
}
}
$data = base64_encode($username)."|".base64_encode($password)."|".$_SERVER["REMOTE_ADDR"]."|".time();
fwrite($file_users, $data."\n");
fclose($file_users);
echo "<script>alert('注册成功!');location.href='login.html';</script>";
}
?>
register.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>用户注册</title>
<link rel="stylesheet" type="text/css" href="Css/style.css">
</head>
<body>
<div class="box">
<div class="top">
<a href="#">注册</a>|<a href="login.html">登录</a>
</div>
<form action="register.php" method="post">
<input type="text" name="username" placeholder="请输入用户名">
<br>
<input type="password" name="password" placeholder="请输入密码">
<br>
<input type="password" name="password1" placeholder="请再次输入密码">
<br>
<input class="login" type="submit" name="register" value="注册">
</form>
</div>
</body>
</html>
新闻添加
add.php
<head>
<title>添加新闻</title>
<link href="Css/style.css" rel="stylesheet" type="text/css">
</head>
<body class="body">
<h1>请输入要添加的新闻内容</h1>
<?php
session_start();
if (isset($_POST['title']) && isset($_POST['content']) && isset($_FILES['filedata'])) {
$article_title = $_POST['title'];
$article_content = $_POST['content'];
$article_img = $_FILES['filedata'];
move_uploaded_file($article_img['tmp_name'], './upload_file/files/' . $article_img['name']);
$data = '0' .'|'. 'upload_file/files/'.$article_img['name'] .'|'. base64_encode($article_title) .'|'. base64_encode($article_content) .'|'.$_SESSION['username'].'|'. time();
$file_news = fopen('Data/news.db', 'a');
fwrite($file_news, $data."\n");
fclose($file_news);
echo "<script>alert('文章添加成功!');location.href='index.php';</script>";
}
?>
<form action="manage.php" method="post" enctype="multipart/form-data">
<p><input type="text" name="title" placeholder="请输入文章标题" maxlength="300"></p>
<p><textarea rows="5" cols="50" placeholder="请输入文章内容" name="content"></textarea></p>
<p>请添加文章封面:<input type="file" name="filedata"></p>
<p><input type="submit" name="submit" value="提交"></p>
</form>
</body>
</html>