0
我是新來的php和混淆這個流程。在我看來,當$ 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)已經運行以來,新的值將被測試。PHP前端控制器流量混亂 - 交換語句只測試過一次
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>
也許有些問題require_once('UTIL/secure_conn.php 「); require_once('util/valid_admin.php');這是觸發你的index.php –