我们实现的管理界面非常简单。创建一个网站界面,该页面可以连接数据库,并且执行一些前台的身份验证。这些代码与第27章中用到的代码非常类似。出于完整性考虑,我们还是在这里给出了这些代码,但对它不做具体的介绍。
管理界面需要用户通过login.php文件进行登录,该文件会将用户带入到管理菜单admin.php中。登录界面如图28-11所示。(为了简洁起见,我们没有给出login.php的代码——该文件与第27章中介绍的几乎一样,如果要查看这个文件,可以在附带的文件中找到这个文件。)管理菜单如图28-12所示。


管理菜单的代码如程序清单28-17所示。
程序清单28-17 admin.php——该脚本将验证管理员身份并允许他访问管理功能
<?php
//include function files for this application
require_once(\'book_sc_fns.php\');
session_start;
if(($_POST[\'username\'])&&($_POST[\'passwd\'])){
//they have just tried logging in
$username=$_POST[\'username\'];
$passwd=$_POST[\'passwd\'];
if(login($username,$passwd)){
//if they are in the database register the user id
$_SESSION[\'admin_user\']=$username;
}else{
//unsuccessful login
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_footer;
?>
这段代码看起来有点熟悉;它与第27章中的一段脚本类似。管理员到达这个页面之后,可以修改密码或者退出——该代码与第27章中的代码完全一样,因此在这里不再介绍。
我们在管理员登录之后通过会话变量admin_user和check_admin_user函数来识别其身份。该函数和其他被管理员脚本调用的函数一样,都可以在函数库admin_fns.php中找到。
如果管理员选择添加一个新的目录或图书,可以根据具体情况进入insert_category_form.php或insert_book_form.php文件。每个脚本都会向管理员提供一个表单,该表单必须由管理员填写。每个表单都由相应的脚本来处理(insert_category.php和insert_book.php),这些脚本将检查表单是否填好并将数据插入到数据库中。在这里,我们只讨论添加图书的脚本,因为添加图书的脚本和添加目录的脚本类似。
insert_book_form.php脚本的输出结果如图28-13所示。

注意图书的目录域是一个HTML的SELECT元素。SELECT的选项来自对get_categories函数的调用,前面我们已经介绍了该函数。
点击\"Add Book\"按钮之后,将触发insert_book.php脚本的执行。该脚本的代码如程序清单28-18所示。
程序清单28-18 insert_book.php——该脚本将验证新书数据并将它添加到数据库
<?php
//include function files for this application
require_once(\'book_sc_fns.php\');
session_start;
do_html_header(\"Adding a book\");
if(check_admin_user){
if(filled_out($_POST)){
$isbn=$_POST[\'isbn\'];
$title=$_POST[\'title\'];
$author=$_POST[\'author\'];
$catid=$_POST[\'catid\'];
$price=$_POST[\'price\'];
$description=$_POST[\'description\'];
if(insert_book($isbn,$title,$author,$catid,$price,$description)){
echo\"<p>Book<em>\".stripslashes($title).\"</em>was added to the
database.</p>\";
}else{
echo\"<p>Book<em>\".stripslashes($title).\"</em>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;
?>
可以看到,该脚本调用了函数insert_book。该函数和其他管理脚本调用的函数一样,都可以在函数库admin_fns.php中找到。
除了添加新目录和新书,管理员还可以编辑和删除它们。我们已经重用了尽可能多的脚本来实现这些功能。当管理员点击管理菜单页面中的\"Go to Main Site\"链接时,将回到index.php中的目录索引,并且与普通用户一样使用同样的脚本接受该索引的导航。
然而,管理导航之间不完全相同:管理员将看到不同的选项,这些选项是根据他们已经注册的会话变量admin_user来确定的。例如,如果我们查看一下本章前面介绍的show_book.php页,将看到一些不同的菜单选项,如图28-14所示。

管理员可以访问该页面中的两个新增选项:\"Edit Item\"和\"Admin Menu\"。注意在页面的右上角我们看不到购物车——取而代之的是一个\"Log Out\"按钮。
这些代码都在程序清单28-8中列出,如下所示:
if(check_admin_user){
display_button(\"edit_book_form.php?isbn=\".$isbn,\"edit-item\",\"Edit Item\");
display_button(\"admin.php\",\"admin-menu\",\"Admin Menu\");
display_button($target,\"continue\",\"Continue\");
}
如果回头看看show_cat.php脚本,将发现它也有这些内置选项。
如果管理员点击\"Edit Item\"按钮,将进入edit_book_form.php脚本。该脚本的输出如图28-15所示。

实际上,这是我们前面用来获取图书详细信息的同一个表单。我们建立了一个指向该表单的选项用来传入和显示现存的图书数据。对于目录,同样如此。要明白为什么目录操作也是如此,请参阅程序清单28-19。
程序清单28-19 admin_fns.php文件中的display_book_form函数——该表单完成了两项工作:插入图书和编辑图书
function display_book_form($book=\'\'){
//This displays the book form.
//It is very similar to the category form.
//This form can be used for inserting or editing books.
//To insert,don\'t pass any parameters.This will set$edit
//to false,and the form will go to insert_book.php.
//To update,pass an array containing a book.The
//form will be displayed with the old data and point to update_book.php.
//It will also add a\"Delete book\"button.
//if passed an existing book,proceed in\"edit mode\"
$edit=is_array($book);
//most of the form is in plain HTML with some
//optional PHP bits throughout
?>
<form method=\"post\"
action=\"<?php echo$edit?\'edit_book.php\':\'insert_book.php\';?>\">
<table border=\"0\">
<tr>
<td>ISBN:</td>
<td><input type=\"text\"name=\"isbn\"
/></td>
</tr>
<tr>
<td>Book Title:</td>
<td><input type=\"text\"name=\"title\"
/></td>
</tr>
<tr>
<td>Book Author:</td>
<td><input type=\"text\"name=\"author\"
/></td>
</tr>
<tr>
<td>Category:</td>
<td><select name=\"catid\">
<?php
//list of possible categories comes from database
$cat_array=get_categories;
foreach($cat_array as$thiscat){
echo\"<option value=\"\".$thiscat[\'catid\'].\"\"\";
//if existing book,put in current catgory
if(($edit)&&($thiscat[\'catid\']==$book[\'catid\'])){
echo\"selected\";
}
echo\">\".$thiscat[\'catname\'].\"</option>\";
}
?>
</select>
</td>
</tr>
<tr>
<td>Price:</td>
<td><input type=\"text\"name=\"price\"
/></td>
</tr>
<tr>
<td>Description:</td>
<td><textarea rows=\"3\"cols=\"50\"
name=\"description\">
<?php echo$edit?$book[\'description\']:\'\';?>
</textarea></td>
</tr>
<tr>
<td<?php if(!$edit){echo\"colspan=2\";}?>>
<?php
if($edit)
//we need the old isbn to find book in database
//if the isbn is being updated
echo\"<input type=\"hidden\"name=\"oldisbn\"
value=\"\".$book[\'isbn\'].\"\"/>\";
?>
<input type=\"submit\"
/>
</form></td>
<?php
if($edit){
echo\"<td>
<form method=\"post\"action=\"delete_book.php\">
<input type=\"hidden\"name=\"isbn\"
value=\"\".$book[\'isbn\'].\"\"/>
<input type=\"submit\"value=\"Delete book\"/>
</form></td>\";
}
?>
</td>
</tr>
</table>
</form>
<?php
}
如果我们传入一个包含图书数据的数组,该表单将显示为编辑模式,并用数组中的数据填充其中的域,如下所示:
<input type=\"text\"name=\"price\"
/><
我们甚至可以得到一个不同的提交按钮。实际上,对编辑表单,我们有两个脚本——一个用来更新图书,而另一个用来删除图书。这两个脚本分别称为edit_book.php和delete_book.php,它们都要相应地更新数据库。
从工作原理来说,目录版本与图书版本的脚本是相同的,但有一点不同的是,当管理员删除一个目录的时候,如果该目录仍然包含有图书,那么该目录不可删除(这需要通过数据库查询进行检查),这避免了任何不正常删除的问题。在第8章中我们讨论过这样的问题。在这种情况下,如果一个目录还有图书包含在内,就被直接删除,那么这些书将成为“孤儿”。我们不知道它们属于哪个目录,也不知道如何查找它们!
以上就是对管理界面的概述。要了解更多信息,请参阅代码——它们都可在附带的文件里找到。