接着上篇继续学习: 《php实现购物车的功能(上)》
7、实现一个管理界面
登录界面
由以下代码实现:
7.1 admin.php
<?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()
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()
function check_admin_user() //检查是否是管理员 { if(isset($_SESSION['admin_user'])) return true; else return false; }
管理主界面
由以下代码实现:
7.4 output_fns.php文件中的函数display_admin_menu()
function display_admin_menu() //输出管理员菜单 { ?> <br /> <a href="https://www.jb51.net/index.php">Go to main site</a><br /> <a href="https://www.jb51.net/insert_category_form.php">Add a new category</a><br /> <a href="https://www.jb51.net/insert_book_form.php">Add a new book</a><br /> <a href="https://www.jb51.net/change_password_form.php">Change admin password</a><br /> <?php } function display_button($target,$image,$alt) //显示按钮 { echo "<div align= \" center \"><a href=https://www.jb51.net/article/\"". $target ."https://www.jb51.net/article/\"> <img src=https://www.jb51.net/article/\"images/". $image .".gif\" alt=https://www.jb51.net/article/\"". $alt ."https://www.jb51.net/article/\" border = \" 0 \" height = \" 50 \" width = \" 135 \" /></a></div>"; }
目录添加
目录添加成功
目录页中可以看出多了Novel目录
由以下代码实现:
7.5 insert_category_form.php
<?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
<?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 ."https://www.jb51.net/article/\" was added to the database.</p>"; } else { echo "<p>Category \"". $catname ."https://www.jb51.net/article/\" 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