0

I am new to php and confused on the flow of this. It seems to me that switch($action) would need to be evaluated more than once when the $action variable gets a new value. But I don't see the switch($action) being evaluated but just once. I used admin_menu.php as an example. What if the user picks <p><a href="index.php?action=show_product_manager">Product Manager</a></p> from admin_menu.php? I don't see where index.php is called afterwards, so I don't see how the new value will be tested since switch($action) has already ran.

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>
dman
  • 10,406
  • 18
  • 102
  • 201
  • Maybe some problem with require_once('util/secure_conn.php'); require_once('util/valid_admin.php'); which is triggering your index.php – Abhishek Saha Oct 26 '12 at 05:35

1 Answers1

0

I just now see the answer....index.php is being called again with: index.php?action=show_order_manager in <p><a href="index.php?action=show_order_manager">Order Manager</a></p>

dman
  • 10,406
  • 18
  • 102
  • 201