code_junkie

Curing the “Back Button Blues”

Nov 13th, 2011
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. function scriptNext($script_id)
  2. // proceed forwards to a new script
  3. {
  4. if (empty($script_id)) {
  5. trigger_error("script id is not defined", E_USER_ERROR);
  6. } // if
  7.  
  8. // get list of screens used in this session
  9. $page_stack = $_SESSION['page_stack'];
  10. if (in_array($script_id, $page_stack)) {
  11. // remove this item and any following items from the stack array
  12. do {
  13. $last = array_pop($page_stack);
  14. } while ($last != $script_id);
  15. } // if
  16.  
  17. // add next script to end of array and update session data
  18. $page_stack[] = $script_id;
  19. $_SESSION['page_stack'] = $page_stack;
  20.  
  21. // now pass control to the designated script
  22. $location = 'http://' .$_SERVER['HTTP_HOST'] .$script_id;
  23. header('Location: ' .$location);
  24. exit;
  25.  
  26. } // scriptNext
  27.  
  28. function scriptPrevious()
  29. // go back to the previous script (as defined in PAGE_STACK)
  30. {
  31. // get id of current script
  32. $script_id = $_SERVER['PHP_SELF'];
  33.  
  34. // get list of screens used in this session
  35. $page_stack = $_SESSION['page_stack'];
  36. if (in_array($script_id, $page_stack)) {
  37. // remove this item and any following items from the stack array
  38. do {
  39. $last = array_pop($page_stack);
  40. } while ($last != $script_id);
  41. // update session data
  42. $_SESSION['page_stack'] = $page_stack;
  43. } // if
  44.  
  45. if (count($page_stack) > 0) {
  46. $previous = array_pop($page_stack);
  47. // reactivate previous script
  48. $location = 'http://' .$_SERVER['HTTP_HOST'] .$previous;
  49. } else {
  50. // no previous scripts, so terminate session
  51. session_unset();
  52. session_destroy();
  53. // revert to default start page
  54. $location = 'http://' .$_SERVER['HTTP_HOST'] .'/index.php';
  55. } // if
  56.  
  57. header('Location: ' .$location);
  58. exit;
  59.  
  60. } // scriptPrevious
  61.  
  62. function initSession()
  63. // initialise session data
  64. {
  65. // get program stack
  66. if (isset($_SESSION['page_stack'])) {
  67. // use existing stack
  68. $page_stack = $_SESSION['page_stack'];
  69. } else {
  70. // create new stack which starts with current script
  71. $page_stack[] = $_SERVER['PHP_SELF'];
  72. $_SESSION['page_stack'] = $page_stack;
  73. } // if
  74.  
  75. // check that this script is at the end of the current stack
  76. $actual = $_SERVER['PHP_SELF'];
  77. $expected = $page_stack[count($page_stack)-1];
  78. if ($expected != $actual) {
  79. if (in_array($actual, $page_stack)) {// script is within current stack, so remove anything which follows
  80. while ($page_stack[count($page_stack)-1] != $actual ) {
  81. $null = array_pop($page_stack);
  82. } // while
  83. $_SESSION['page_stack'] = $page_stack;
  84. } // if
  85. // set script id to last entry in program stack
  86. $actual = $page_stack[count($page_stack)-1];
  87. $location = 'http://' .$_SERVER['HTTP_HOST'] .$actual;
  88. header('Location: ' .$location);
  89. exit;
  90. } // if
  91.  
  92. ... // continue processing
  93.  
  94. } // initSession
  95.  
  96. if ($_POST) {
  97. process_input($_POST);
  98. header("Location: $_SERVER[HTTP_REFERER]");
  99. exit;
  100. }
Add Comment
Please, Sign In to add comment