这是一个作业

结构:

basedir:存放上下界面

​ header.php 上

​ footer.php下

data:存放新闻用户信息

​ tupian 存放所有图片

​ xinwenwenjian存放所有新闻的信息

​ yonghuxinxiwenjian存放所有新闻文件

lib:存放功能界面

​ index.php 主页包含界面

​ login.php登陆界面

​ register.php注册前端

​ registerhoutai.php注册后端

​ shanchuxinwen.php 删除新闻前端

​ shanchuxinwenhouduan.php删除新闻后端

​ shanchuangtouxiang.php 上传头像前端

​ shanchuangtouxianghoutai.php上传头像后端

​ xinwenzhanshi.php新闻展示界面

​ xinwenzhanshimore.php新闻展示更多信息

​ xiugaimima.php 修改密码前端

​ xiugaimimamore.php 修改密码后端

​ yonghutuichu.php 用户退出后端

​ zengjiaxinwen.php 增加新闻前端

​ zengjiaxinwenhoutai.php增加新闻后端

主页包含界面:

1
2
3
4
5
<?phpRequire_once '../basedir/header.php';?>

<?phpRequire_once "xinwenzhanshi.php";?>

<?phpRequire_once '../basedir/footer.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
73
74
75
76
<?php

Require_once '../basedir/header.php';

?>

<!DOCTYPE html>
<html>
<head > <meta charset="utf-8"></head>
<body>

<hr>

<div style="width:100%;text-align:center">
<tr>
<h1>骚年输入的你的编号</h1>
<form action="login.php" method="post" >
<p><input type="text" name="name" placeholder="手机号/邮箱/用户名"></p>
<p><input type="password" name="passwd"></p>
<p><input class="input_code" type="text" name="verifycode" placeholder="验证码"></p>
<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">

<p><input type="submit" name="提交"></p>
</form>

</tr>
<tr>
<td align="right">
<a href="../lib/register.php"><button align="right">用户注册</button></a>
</td>
</tr>
</div>
</body>
</html>
<?php

Require_once '../basedir/footer.php';

?>

<?php
error_reporting(E_ALL^E_NOTICE);

$uname = base64_encode($_POST['name']);

$upass = md5($_POST['passwd']);

if ($uname && $upass){

// echo base64_decode($uname);

$f = fopen('../data/yonghuxinxiwenjian/yh.db','r');
while (!feof($f)){
$data = fgets($f);
$arr = explode('|',$data);

if(count($arr) !=4 ){
continue;

}
// echo base64_decode($arr[0]);

if ($arr[0] == $uname && $arr[1] == $upass){

$idd = base64_decode($arr[0]);
session_start();
$_SESSION['username']=$idd;

echo "<script>alert('登陆成功!将跳转到主页!');location.href='index.php';</script>";
break;
}

}
echo "<script>alert('用户名或者密码错误,请重新登录!');location.href='login.php';</script>";
}
?>

注册功能

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
前端
<?php

Require_once '../basedir/header.php';

?>

<hr>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>快来和我一起欢乐</title>
</head>
<body>

<div style="width:100%;text-align:center">
<tr>


<form action="registerhoutai.php" method="post" enctype="multipart/form-data">
<h3><p>用户名</p></h3>
<p><input type="text" name="name"></p>
<h3><p>密码</p></h3>
<p><input type="password" name="passwd"></p>
<h3><p>再次确认密码</p></h3>
<p><input type="password" name="passwd2"></p>
<h3><p>输入你的邮箱</p></h3>
<p><input type="text" name="youxiang"></p>
<h3><p>上传你的头像</p></h3>
<p> <input type="file" name="file"></p>
<p><input type="submit" name="提交"></p>
</form>


</tr>

</body>
</html>


<?php

Require_once '../basedir/footer.php';

?>

删除新闻功能

1
2


上传头像功能

新闻展示界面:

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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<html>
<style >
.th{
vertical-align: middle;
}


</style>

<div style="width:100%;text-align:center">
<h1>新闻界面</h1>
</div>


<hr>

<?php
session_start();
error_reporting(E_ALL^E_NOTICE);
if(isset($_SESSION['username'])){
echo <<<EOF
<table align="center" width="600">
<tr ><th id="sctx"><a href="shangchuantouxiang.php">上传头像</a></th><th id="zjxw"><a href="zengjiaxinwen.php">增加新闻</a></th><th id="scxw"><a href="shanchuxinwen.php">删除新闻</th><th id="xgmm"><a href="xiugaimima.php">修改密码</th></tr>
</table>

EOF;

}


?>



<table align="center" border="1" width="1200">
<tr>
<th> <h2>热度</h2> </th>
<th> <h2>标题</h2> </th>
<th> <h2>作者</h2> </th>
<th> <h2>时间</h2> </th>
</tr>

<?php
$f = fopen('../data/xinwenwenjian/new.db','r');
while (!feof($f)){



$str = fgets($f);



$shuzu = explode("|",$str);
//echo "长度为:".strlen($shuzu[0]);
if(count($shuzu) !=6){
continue;
}

$shuzu = explode('|',$str);//按照|进行分割
$redu = $shuzu[0];
$biaoti = base64_decode($shuzu[1]);
$zuozhe = base64_decode($shuzu[2]);
$shijian = base64_decode($shuzu[3]);
$tupian = base64_decode($shuzu[4]);
$jianjie= mb_substr(base64_decode($shuzu[5]),0,150);


echo <<<EOF
<tr>
<td align="center">{$redu}</td>
<td align="center">{$biaoti}</td>
<td align="center">{$zuozhe}</td>
<td align="center">{$shijian}</td>
</tr>

<tr>
<td align="center" height: 30px; width: 300px; text-align: center;>
<img height="150px" src={$tupian} >
</td>
<td colspan="3" align="center" colspan="3"><a href="xinwenzhanshimore.php?action={$biaoti}">{$jianjie}</a> </td>
</tr>
EOF;


}
?>

</table>

</html>
后端
<?php
if ($_POST['passwd'] != $_POST['passwd2']){
echo "<script>alert('两次输入的密码不正确')</script>";
die();
}
$f = fopen("../data/yonghuxinxiwenjian/yh.db","r");
while(!feof($f)){
$zifuchuan = fgets($f);
$zifuchuan = explode("|",$zifuchuan);
// echo $_POST['name'];
// echo base64_decode($zifuchuan[0]);
if($_POST['name'] == base64_decode($zifuchuan[0])){
echo "<script>alert('此账号已被注册');location.href='register.php';</script>";
die();
}
}
$name = base64_encode($_POST['name']);
$passwd = md5($_POST['passwd']);
$youxiang = base64_encode($_POST['youxiang']);


$filename= $_FILES['file']['name'];

$filehouzui = explode('.',$filename); //根据.来分割
$filezhenshiming = $_POST['name'].'.'.$filehouzui[1];//将分割出来的后缀和.合并到一起
$filebaocunlujing = "../data/yonghuxinxiwenjian/".$filezhenshiming;//此处为保存路径
move_uploaded_file($_FILES['file']['tmp_name'],$filebaocunlujing);
$filebaocunlujing = base64_encode($filebaocunlujing);


$f = fopen("../data/yonghuxinxiwenjian/yh.db","a+");
$shuju = "$name|$passwd|$youxiang|$filebaocunlujing"."\n";
fwrite($f ,$shuju );
fclose($f);

session_start();
$_SESSION['username']=$_POST['name'];
echo "<script>alert('恭喜你注册成功!即将跳转到主面!');location.href='index.php';</script>";

?>

修改密码功能

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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
前端
<?php
session_start();
error_reporting(E_ALL^E_NOTICE);
if(!isset($_SESSION['username'])){
echo "<script>alert('请登录后查看!');location.href='login.php';</script>";
}
?>



<?php

Require_once '../basedir/header.php';

?>


<html>
<div style="width:100%;text-align:center">
<h1>修改密码</h1>
</div>

<hr>

<?php


if(isset($_SESSION['username'])){
echo <<<EOF
<table align="center" width="600">
<tr ><th id="sctx"><a href="shangchuantouxiang.php">上传头像</a></th><th id="xwjm"><a href="zengjiaxinwen.php">增加新闻</th><th id="scxw"><a href="shanchuxinwen.php">删除新闻<th id="xgmm"><a href="index.php">新闻界面</th></tr>
</table>

EOF;

}
?>
</html>

<div style="width:100%;text-align:center">
<tr>

<form action="xiugaimimahoutai.php" method="post" >
<h3><p>原密码</p></h3>
<p><input type="password" name="ypasswd" ></p>
<h3><p>修密码</p></h3>
<p><input type="password" name="tpasswd" ></p>
<h3><p>再次输入密码</p></h3>
<p><input type="password" name="tpasswd2" ></p>
<p></p>
<p><input type="submit" name="提交"></p>

</form>
</tr>

<?php

Require_once '../basedir/footer.php';

?>
后端
<?php
session_start();
error_reporting(E_ALL^E_NOTICE);

$uname = base64_encode($_SESSION['username']);

$upass = md5($_POST['ypasswd']);

$q = 0;

if ($uname && $upass){
// echo base64_decode($uname);
$f = fopen('../data/yonghuxinxiwenjian/yh.db','r');
while (!feof($f)){
$data = fgets($f);
$arr = explode('|',$data);

if(count($arr) !=4 ){
continue;
}
// echo $arr[0];
// echo ':';
// echo $uname;
// echo '<br>';
// echo $arr[1];
// echo ':';
// echo $upass;
// echo '<br>';
if ($arr[0] == $uname && $arr[1] == $upass){
$q = 1;
}
}
if($q == 0){
echo "<script>alert('原密码错误!');location.href='xiugaimima.php';</script>";
die();
}
}

$a = 0;

if ($_POST['tpasswd'] != $_POST['tpasswd2']){
echo "<script>alert('两次输入的密码不正确');location.href='xiugaimima.php';</script>";
die();
}else{
$a=1;
}

if($q && $a){

$filePath = '../data/yonghuxinxiwenjian/yh.db';
$target= md5($_POST['ypasswd']);
$result = null;
$fileCont = file_get_contents($filePath);

$target = base64_encode($_SESSION['username']).'|'.$target;
// echo $target;
// echo '<br>';
// echo $fileCont;
// echo '<br>';
$targetIndex = strpos($fileCont, $target); #查找目标字符串的坐标
echo $targetIndex;
if ($targetIndex !== false) {

#找到target的前一个换行符
$preChLineIndex = strrpos(substr($fileCont, 0, $targetIndex+1), "\n");
var_dump($preChLineIndex);
// var_dump($targetIndex);
// var_dump($preChLineIndex);

#找到target的后一个换行符
$AfterChLineIndex = strpos(substr($fileCont, $targetIndex), "\n") + $targetIndex;

$tihuanhang = substr($fileCont,$preChLineIndex,$AfterChLineIndex);

$target2 = base64_encode($_SESSION['username']).'|'.md5($_POST['tpasswd']);
var_dump($target2);
var_dump($target);

$tihuanhang = str_replace($target,$target2,$tihuanhang);


var_dump($tihuanhang);
var_dump($AfterChLineIndex);
if (($preChLineIndex !== false || $preChLineIndex == 0) && $AfterChLineIndex !== false) {
#重新写入删掉指定行后的内容
$result = substr($fileCont, 0, $preChLineIndex + 1) .$tihuanhang. substr($fileCont, $AfterChLineIndex + 1);
file_put_contents($filePath, $result);
$fp = fopen($filePath, "w+");
fwrite($fp, $result);
fclose($fp);
}

echo "<script>alert('密码修改成功');location.href='xiugaimima.php';</script>";

}


}


?>

用户退出功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
后端
<?php
session_start();
error_reporting(E_ALL^E_NOTICE);
unset($_SESSION['username']);
?>


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script>alert('登出成功!将跳转到主页!');location.href='index.php';</script>

</head>
<body>

</body>
</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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
前端
<?php
session_start();
error_reporting(E_ALL^E_NOTICE);
if(!isset($_SESSION['username'])){
echo "<script>alert('请登录后查看!');location.href='login.php';</script>";
}
?>
<?php

Require_once '../basedir/header.php';

?>




<html>
<div style="width:100%;text-align:center">
<h1>增加新闻</h1>
</div>

<hr>

<?php

session_start();if(isset($_SESSION['username'])){
echo <<<EOF

<table align="center" width="600">
<tr ><th id="sctx"><a href="shangchuantouxiang.php">上传头像</a></th><th id="xwjm"><a href="index.php">新闻界面</th><th id="scxw"><a href="shanchuxinwen.php">删除新闻</th><th id="xgmm"><a href="xiugaimima.php">修改密码</th></tr>
</table>

EOF;

}
?>


<div style="width:100%;text-align:center">
<tr>
<h3>骚年请上传新闻</h3>
<form action="zengjiaxinwenhoutai.php" method="post" enctype="multipart/form-data">
<h5><p>热度:<br><input type="text" name="redu" id="redu" ></p></h5>

<h5><p>标题:<br><input type="text" name="biaoti" id="biaoti" ></p></h5>

<p><input type="file" name="Filedata" ><br></p>


<h5><p>简介:<br><textarea name="jianjie" placeholder="输入新闻内容" rows="10" cols="50" ></textarea><br><br></p></h5>
<p><input type="submit" name="提交"></p>
</form>

</tr>

</div>

<body>
</html>






<?php

Require_once '../basedir/footer.php';

?>
后端

<?php
error_reporting(E_ALL^E_NOTICE);
session_start();

$redu = $_POST["redu"];
$biaoti = base64_encode(htmlspecialchars($_POST["biaoti"]));
$zuozhe = base64_encode($_SESSION['username']);

$time = time(); //时间戳
$shijian = base64_encode(date( 'Y-m-d H:i:s', $time)); //生成带格式的日期

//上传文件部分
$name = explode('.',$_FILES['Filedata']['name']); //取出上传文件的后缀
var_dump($_FILES['Filedata']['name']);
$houzhui = '.'.end($name);//将后缀和.合起来,变成.xxx

var_dump($houzhui);

$wjm = $_SESSION['username'].time().$houzhui;//生成一个文件名:登录名+时间戳+后缀
$lujing = "../data/xinwenwenjian/".$wjm; //将路径和文件名合并在一起。生成保存路径
move_uploaded_file($_FILES['Filedata']['tmp_name'],$lujing);//将文件从临时文件转移到自己定义的路径中
$lujing = base64_encode("{$lujing}");



$jianjie = base64_encode(htmlspecialchars($_POST["jianjie"]));

$qwe = "$redu|$biaoti|$zuozhe|$shijian|$lujing|$jianjie"."\n";

$f = fopen("../data/xinwenwenjian/new.db","a+");

fwrite($f,$qwe);

echo "<script>alert('新闻上传成功');location.href='zengjiaxinwen.php';</script>";
?>

点击显示更多新闻

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
<?php

Require_once '../basedir/header.php';

?>

<?php
$action = $_GET['action'];

?>

<div style="width:100%;text-align:center">
<h1>新闻详情</h1>
</div>

<hr>

<table align="center" width="600">
<tr ><th id="xwjm"><a href="index.php">新闻界面</th>
</table>

<?php
$f = fopen('../data/xinwenwenjian/new.db','r');
while (!feof($f)){
$str = fgets($f);

$shuzu = explode("|",$str);

if(count($shuzu) !=6){
continue;
}

$shuzu = explode('|',$str);//按照|进行分割

if(trim(base64_decode($shuzu[1])) == $action){

$biaoti = base64_decode($shuzu[1]);
$jianjie= base64_decode($shuzu[5]);

echo <<<EOF
<table align="center">
<tr>
<td align="center"><h2>{$biaoti}</h2></td>
</tr>

<tr>
<td colspan="3" align="center" ><h3>{$jianjie}</h3> </td>
</tr>
</table>

EOF;
}

}
?>



<?php

Require_once '../basedir/footer.php';

?>