有没有偶然发现一个你觉得很有价值但解释不正确的教程?这就是我的困境。我知道本教程有一些价值,但我就是无法理解。
- 你在哪里调用每个函数?
- 应该首先调用哪个函数,然后调用哪个函数,然后调用哪个函数?
- 是否会在应用程序的所有文件中调用所有函数?
- 有谁知道治疗“后退按钮忧郁症”的更好方法?
我想知道这是否会引发一些包括文章作者在内的良好对话。我特别感兴趣的部分是控制后退按钮,以防止在按下后退按钮时将重复条目输入数据库。基本上,您希望通过在应用程序中执行脚本期间调用以下三个函数来控制后退按钮。教程中并不清楚调用函数的确切顺序(参见上面的问题)。
所有向前移动都是使用我的 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
当任何脚本完成其处理时,它通过调用我的 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
每当一个脚本被激活,可以是通过scriptNext或者scriptPrevious函数,也可以是因为浏览器中的BACK按钮,它会调用下面的函数根据程序栈的内容来验证它是当前脚本,并如果不是,请采取适当的措施。
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 数组中的最后一个条目。这是预期的情况。全场畅饮!