7、实现一个管理界面
登录界面
由以下代码实现:
7.1 admin.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
|
<?php
/**
* @author switch
* @copyright 2015
* 主管理菜单
*/
//require_once语句和require语句完全相同,唯一区别是php会检查该文件是否已经被包含过,如果是则不会再次包含。
require_once ( 'book_sc_fns.php' );
session_start();
if ((@ $_post [ 'username' ]) && (@ $_post [ 'passwd' ])) //尝试登陆
{
$username = $_post [ 'username' ];
$passwd = $_post [ 'passwd' ];
if (login( $username , $passwd ))
{
$_session [ 'admin_user' ] = $username ;
}
else
{
do_html_header( "problem:" );
echo "<p>you could not be logged in.<br />
you must be logged in to view this page.</p>";
do_html_url( 'login.php' , 'login' );
do_html_footer();
exit ;
}
}
do_html_header( "administration" );
if (check_admin_user())
{
display_admin_menu();
}
else
{
echo "<p>you are not authorized to enter the administration area.</p>" ;
do_html_url( 'login.php' , 'login' );
}
do_html_footer();
?>
|
7.2 user_auth_fns.php文件中的函数login()
?
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
function login( $username , $password ) //登录
{
$conn = db_connect(); //连接数据库
if (! $conn )
return 0;
//检查用户名唯一性
$query = "select * from admin where username='" . $username ."'
and password = sha1( '". $password ."' )";
$result = $conn ->query( $query );
if (! $result )
return 0;
if ( $result ->num_rows > 0)
return 1;
else
return 0;
}
|
7.3 user_auth_fns.php文件中的函数check_admin_user()
?
1
|
2
3
4
5
6
7
|
function check_admin_user() //检查是否是管理员
{
if (isset( $_session [ 'admin_user' ]))
return true;
else
return false;
}
|
管理主界面
由以下代码实现:
7.4 output_fns.php文件中的函数display_admin_menu()
?
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
function display_admin_menu() //输出管理员菜单
{
?>
<br />
<a href= "index.php" >go to main site</a><br />
<a href= "insert_category_form.php" >add a new category</a><br />
<a href= "insert_book_form.php" >add a new book</a><br />
<a href= "change_password_form.php" >change admin password</a><br />
<?php
}
function display_button( $target , $image , $alt ) //显示按钮
{
echo "<div align= \\" center \\"><a href=\\"" . $target ."\\">
<img src=\\ "images/" . $image .".gif\\"
alt=\\ "" . $alt ."\\" border = \\" 0 \\" height = \\" 50 \\"
width = \\ " 135 \\" /></a></div>" ;
}
|
由以下代码实现:
7.5 insert_category_form.php
?
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?php
/**
* @author switch
* @copyright 2015
* 允许管理员向数据库中添加一个目录的表格
*/
//require_once语句和require语句完全相同,唯一区别是php会检查该文件是否已经被包含过,如果是则不会再次包含
require_once ( 'book_sc_fns.php' );
session_start();
do_html_header();
if (check_admin_user())
{
display_category_form();
do_html_url( "admin.php" , "back to administrtion menu" );
}
else
{
echo "<p>you are not authorized to enter the administation area.</p>" ;
}
do_html_footer();
?>
|
7.6 insert_category.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
|
<?php
/**
* @author switch
* @copyright 2015
* 向数据库中插入新目录
*/
//require_once语句和require语句完全相同,唯一区别是php会检查该文件是否已经被包含过,如果是则不会再次包含
require_once ( 'book_sc_fns.php' );
session_start();
do_html_header( "adding a category" );
if (check_admin_user())
{
if (filled_out( $_post ))
{
$catname = $_post [ 'catname' ];
if (insert_category( $catname ))
{
echo "<p>category \\"" . $catname . "\\" was added to the database.</p>" ;
}
else
{
echo "<p>category \\"" . $catname . "\\" could not be added to the database.</p>" ;
}
}
else
{
echo "<p>you have not filled out the form. please try again.</p>" ;
}
do_html_url( "admin.php" , "back to administration menu" );
}
else
{
echo "<p>you are not authorised to view this page.</p>" ;
}
do_html_footer();
?>
|
管理员目录界面
目录编辑界面-可更新,删除
目录更新成功
目录主界面可以看到该目录更改成功
由以下代码实现:
7.7 edit_category_form.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
|
<?php
/**
* @author switch
* @copyright 2015
* 管理员编辑目录的表单
*/
//require_once语句和require语句完全相同,唯一区别是php会检查该文件是否已经被包含过,如果是则不会再次包含。
require_once ( 'book_sc_fns.php' );
session_start();
do_html_header( "edit category" );
if (check_admin_user())
{
if ( $catname = get_category_name( $_get [ 'catid' ]))
{
$catid = $_get [ 'catid' ];
$cat = compact( 'catname' , 'catid' );
display_category_form( $cat );
}
else
{
echo "<p>could not retrieve category details.</p>" ;
}
do_html_url( "admin.php" , "back to administration menu" );
}
else
{
echo "<p>you are not authorized to enter the administration area.</p>" ;
}
do_html_footer();
?>
|
7.8 edit_category.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
/**
* @author switch
* @copyright 2015
* 更新数据库中的目录
*/
//require_once语句和require语句完全相同,唯一区别是php会检查该文件是否已经被包含过,如果是则不会再次包含。
require_once ( 'book_sc_fns.php' );
session_start();
do_html_header( "updating category" );
if (check_admin_user())
{
if (filled_out( $_post ))
{
if (update_category( $_post [ 'catid' ], $_post [ 'catname' ]))
{
echo "<p>category was updated.</p>" ;
}
else
{
echo "<p>category could not be updated.</p>" ;
}
}
else
{
echo "<p>you have not filled out the form. please try again.</p>" ;
}
do_html_url( "admin.php" , "back to administration menu" );
}
else
{
echo "<p>you are not authorised to view this page.</p>" ;
}
do_html_footer();
?>
|
7.9 admin_fns.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|