PHP 前端控制器流混淆 - switch 语句只测试过一次



我是 php 的新手,对此流程感到困惑。在我看来,当 $action switch($action) 变量获得新值时需要多次评估 switch()。但是我没有看到正在评估的开关($action),而只是一次。我以admin_menu.php为例。如果用户从admin_menu.php中选择<p><a href="index.php?action=show_product_manager">Product Manager</a></p>怎么办?我没有看到之后在哪里调用 index.php,所以我看不到如何测试新值,因为 switch($action) 已经运行了。

localhost ch21_ex2 # cat index.php 
<?php
// Start session management and include necessary functions
session_start();
require_once('model/database.php');
require_once('model/admin_db.php');
// Get the action to perform
if (isset($_POST['action'])) {
$action = $_POST['action'];
} else if (isset($_GET['action'])) {
$action = $_GET['action'];
} else {
$action = 'show_admin_menu';
}
// If the user isn't logged in, force the user to login
if (!isset($_SESSION['is_valid_admin'])) {
$action = 'login';
}
// Perform the specified action
switch($action) {
case 'login':
$email = $_POST['email'];
$password = $_POST['password'];
if (is_valid_admin_login($email, $password)) {
$_SESSION['is_valid_admin'] = true;
include('view/admin_menu.php');
} else {
$login_message = 'You must login to view this page.';
include('view/login.php');
}
break;
case 'show_admin_menu':
include('view/admin_menu.php');
break;
case 'show_product_manager':
include('view/product_manager.php');
break;
case 'show_order_manager':
include('view/order_manager.php');
break;
case 'logout':
$_SESSION = array();   // Clear all session data from memory
session_destroy();     // Clean up the session ID
$login_message = 'You have been logged out.';
include('view/login.php');
break;
}
?>localhost ch21_ex2 # firefox 'view/admin_menu.php'
localhost ch21_ex2 # cat 'view/admin_menu.php'
<?php
require_once('util/secure_conn.php');  // require a secure connection
require_once('util/valid_admin.php');  // require a valid admin user
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My Guitar Shop</title>
<link rel="stylesheet" type="text/css" href="main.css"/>
</head>
<body>
<div id="page">
<div id="header">
<h1>My Guitar Shop</h1>
</div>
<div id="main">
<h1>Admin Menu</h1>
<p><a href="index.php?action=show_product_manager">Product Manager</a></p>
<p><a href="index.php?action=show_order_manager">Order Manager</a></p>
<p><a href="index.php?action=logout">Logout</a></p>
</div><!-- end main -->
</div><!-- end page -->
</body>
</html>

我现在看到了答案....index.php 再次被调用,<p><a href="index.php?action=show_order_manager">Order Manager</a></p>:index.php?action=show_order_manager

相关内容

  • 没有找到相关文章

最新更新