【发布时间】:2010-09-30 09:49:30
【问题描述】:
曾经偶然发现一个您认为很有价值但解释不正确的教程?这就是我的困境。我知道THIS TUTORIAL 有一些价值,但我就是不明白。
- 你在哪里调用每个函数?
- 应该调用哪个函数 第一个,哪个下一个,哪个 第三个?
- 是否会在应用程序的所有文件中调用所有函数?
- 有人知道治疗“后退按钮忧郁症”的更好方法吗?
我想知道这是否会引发一些包括文章作者在内的良好对话。我特别感兴趣的部分是控制后退按钮,以防止在按下后退按钮时将重复条目输入数据库。基本上,您希望通过在应用程序中执行脚本期间调用以下三个函数来控制后退按钮。教程中并不清楚调用函数的确切顺序(请参阅上面的问题)。
所有向前移动都是由 使用我的 scriptNext 函数。这是 在当前脚本中调用 为了激活新脚本。
function scriptNext($script_id) // proceed forwards to a new script { if (empty($script_id)) { trigger_error("script id is not defined", E_USER_ERROR); } // if // get list of screens used in this session $page_stack = $_SESSION['page_stack']; if (in_array($script_id, $page_stack)) { // remove this item and any following items from the stack array do { $last = array_pop($page_stack); } while ($last != $script_id); } // if // add next script to end of array and update session data $page_stack[] = $script_id; $_SESSION['page_stack'] = $page_stack; // now pass control to the designated script $location = 'http://' .$_SERVER['HTTP_HOST'] .$script_id; header('Location: ' .$location); exit; } // scriptNext当任何脚本完成时 处理它通过调用 my scriptPrevious 函数。这会 从末尾删除当前脚本 堆栈数组并重新激活 数组中的前一个脚本。
function scriptPrevious() // go back to the previous script (as defined in PAGE_STACK) { // get id of current script $script_id = $_SERVER['PHP_SELF']; // get list of screens used in this session $page_stack = $_SESSION['page_stack']; if (in_array($script_id, $page_stack)) { // remove this item and any following items from the stack array do { $last = array_pop($page_stack); } while ($last != $script_id); // update session data $_SESSION['page_stack'] = $page_stack; } // if if (count($page_stack) > 0) { $previous = array_pop($page_stack); // reactivate previous script $location = 'http://' .$_SERVER['HTTP_HOST'] .$previous; } else { // no previous scripts, so terminate session session_unset(); session_destroy(); // revert to default start page $location = 'http://' .$_SERVER['HTTP_HOST'] .'/index.php'; } // if header('Location: ' .$location); exit; } // scriptPrevious每当一个脚本被激活时, 既可以通过脚本下一个 或 scriptPrevious 函数,或 因为在 浏览器,它将调用以下 功能来验证它是 当前脚本根据 程序堆栈的内容并取 如果不是,则采取适当的行动。
function initSession() // initialise session data { // get program stack if (isset($_SESSION['page_stack'])) { // use existing stack $page_stack = $_SESSION['page_stack']; } else { // create new stack which starts with current script $page_stack[] = $_SERVER['PHP_SELF']; $_SESSION['page_stack'] = $page_stack; } // if // check that this script is at the end of the current stack $actual = $_SERVER['PHP_SELF']; $expected = $page_stack[count($page_stack)-1]; if ($expected != $actual) { if (in_array($actual, $page_stack)) {// script is within current stack, so remove anything which follows while ($page_stack[count($page_stack)-1] != $actual ) { $null = array_pop($page_stack); } // while $_SESSION['page_stack'] = $page_stack; } // if // set script id to last entry in program stack $actual = $page_stack[count($page_stack)-1]; $location = 'http://' .$_SERVER['HTTP_HOST'] .$actual; header('Location: ' .$location); exit; } // if ... // continue processing } // initSession采取的行动取决于是否 当前脚本存在于 程序堆栈与否。有三种 可能性:
- 当前脚本不在 $page_stack 数组中,在这种情况下它是 不允许继续。相反,它是 替换为位于 数组的末尾。
- 当前脚本位于 $page_stack 数组,但它不是 最后一个条目。在这种情况下,所有 数组中的以下条目是 已删除。
- 当前脚本是最后一个条目 在 $page_stack 数组中。这是 预期的情况。全部喝完 圆!
【问题讨论】: