固化"后退按钮布鲁斯" [英] Curing the "Back Button Blues"

查看:106
本文介绍了固化"后退按钮布鲁斯"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

曾经无意中发现,你觉得是很有价值的,但不是很恰当解释的教程?这是我的困境。我知道本教程有一定的价值,但我不能得到它。


  1. 你在哪里调用每个功能?

  2. 哪些功能应该被称为
    第一和下一个,并且其中
    第三?

  3. 将所有功能中的所有文件被调用的应用程序?

  4. 有谁知道一个更好的办法治愈后退按钮蓝军?

我不知道这是否会激起一些很好的交谈,包括文章的作者。我在被控制的返回按钮,以prevent形式重复条目到时后退按钮是pressed数据库特别感兴趣的部分。基本上,你想通过脚本的应用程序中的执行过程中调用下面三个函数来控制后退按钮。在究竟要调用的函数(见上面的问题)是不是从本教程清晰。


  

所有向前移动被执行
  用我的scriptNext功能。这是
  所谓在当前脚本中
  为了激活新的脚本。

 函数scriptNext($ SCRIPT_ID)
//继续转发到一个新的脚本
{
   如果(空($ SCRIPT_ID)){
      trigger_error(脚本ID没有定义,E_USER_ERROR);
   } //如果   //获取在这个环节所使用的画面列表
   $ page_stack = $ _SESSION ['page_stack'];
   如果(in_array($ SCRIPT_ID,$ page_stack)){
      //删除此项目,并从堆栈阵列的下列项目
      做{
         $最后= array_pop($ page_stack);
      }而($持续= $ SCRIPT_ID!);
   } //如果   //下一个脚本添加到阵列和更新会话数据结束
   $ page_stack [] = $ SCRIPT_ID;
   $ _SESSION ['page_stack'] = $ page_stack;   //现在控制传递给指定的脚本
   $位置=的http://。$ _ SERVER ['HTTP_HOST'] $ SCRIPT_ID;
   头('位置:'$位置);
   出口;} // scriptNext

在任何脚本已经完成它的
  处理它通过调用终止我的
  脚本previous功能。这会
  从最终删除当前脚本
  堆叠阵列并重新启动
  previous脚本数组中

 功能脚本previous()
//回到previous脚本(如PAGE_STACK定义)
{
   //获取当前脚本的ID
   $ SCRIPT_ID = $ _ SERVER ['PHP_SELF'];   //获取在这个环节所使用的画面列表
   $ page_stack = $ _SESSION ['page_stack'];
   如果(in_array($ SCRIPT_ID,$ page_stack)){
      //删除此项目,并从堆栈阵列的下列项目
      做{
         $最后= array_pop($ page_stack);
      }而($持续= $ SCRIPT_ID!);
      //更新会话数据
      $ _SESSION ['page_stack'] = $ page_stack;
   } //如果   如果(计数($ page_stack)大于0){
      $previous = array_pop($ page_stack);
      //重新previous脚本
      $位置=的http://。$ _ SERVER ['HTTP_HOST'] $previous;
   }其他{
      //没有previous脚本,因此终止会话
      session_unset();
      session_destroy();
      //恢复为默认启动页面
      $位置=的http://$ _ SERVER ['HTTP_HOST']'/的index.php';。
   } //如果   头('位置:'$位置);
   出口;} //脚本previous


  
  

每当一个脚本被激活,这
  可以是通过scriptNext
  或脚本previous功能,或
  因为在返回按钮的
  浏览器,它会调用以下
  函数来验证它是
  根据当前的脚本
  该方案的内容栈和取
  采取适当的行动,如果它不是。

 函数initSession()
//初始化会话数据
{
   //获取程序堆栈
   如果(使用isset($ _ SESSION ['page_stack'])){
      //利用现有栈
      $ page_stack = $ _SESSION ['page_stack'];
   }其他{
      //创建一个与当前的脚本启动新的堆栈
      $ page_stack [] = $ _ SERVER ['PHP_SELF'];
      $ _SESSION ['page_stack'] = $ page_stack;
   } //如果   //检查这个脚本是在当前堆栈的末尾
   $实际= $ _ SERVER ['PHP_SELF'];
   预计$ = $ page_stack [计数($ page_stack)-1];
   如果($预期!= $实际){
      如果(in_array($实际,$ page_stack)){//脚本是当前堆栈中,因此它遵循删除任何东西
      而($ page_stack [计数($ page_stack)-1]!= $实际){
            $空= array_pop($ page_stack);
         } //而
         $ _SESSION ['page_stack'] = $ page_stack;
      } //如果
      //设置的脚本ID在程序堆栈最后一个条目
      $实际= $ page_stack [计数($ page_stack)-1];
      $位置=的http://。$ _ ['HTTP_HOST']服务器$实际;
      头('位置:'$位置);
      出口;
   } //如果   ... //继续处理} // initSession


  
  

所采取的行动取决于是否
  当前脚本中的存在
  程序栈或没有。有三种
  可能性:


  
  

      
  • 当前脚本不是$ page_stack阵列中,在这种情况下,它是
      不允许继续。相反,它是
      通过其在所述脚本替换
      数组的结束。

  •   
  • 当前脚本是在
      $ page_stack数组,但它不是
      最后一个条目。在这种情况下,所有
      以下数组中的条目
      删除。

  •   
  • 当前脚本中的最后一项
      在$ page_stack阵列。这是
      预期的情况。所有饮料
      圆!

  •   


解决方案

这是一个很好的讨论,但更多的,你应该寻找到邮政重定向获取(PRG)又称点获取POST之后。

http://www.theserverside.com/patterns/thread.tss?的thread_id = 20936

Ever stumbled on a tutorial that you feel is of great value but not quite explained properly? That's my dilemma. I know THIS TUTORIAL has some value but I just can't get it.

  1. Where do you call each function?
  2. Which function should be called first and which next, and which third?
  3. Will all functions be called in all files in an application?
  4. Does anyone know of a better way cure the "Back Button Blues"?

I'm wondering if this will stir some good conversation that includes the author of the article. The part I'm particularly interested in is controlling the back button in order to prevent form duplicate entries into a database when the back button is pressed. Basically, you want to control the back button by calling the following three functions during the execution of the scripts in your application. In what order exactly to call the functions (see questions above) is not clear from the tutorial.

All forwards movement is performed by using my scriptNext function. This is called within the current script in order to activate the new script.

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

When any script has finished its processing it terminates by calling my scriptPrevious function. This will drop the current script from the end of the stack array and reactivate the previous script in the array.

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

Whenever a script is activated, which can be either through the scriptNext or scriptPrevious functions, or because of the BACK button in the browser, it will call the following function to verify that it is the current script according to the contents of the program stack and take appropriate action if it is not.

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

The action taken depends on whether the current script exists within the program stack or not. There are three possibilities:

  • The current script is not in the $page_stack array, in which case it is not allowed to continue. Instead it is replaced by the script which is at the end of the array.
  • The current script is in the $page_stack array, but it is not the last entry. In this case all following entries in the array are removed.
  • The current script is the last entry in the $page_stack array. This is the expected situation. Drinks all round!

解决方案

That is a good discussion but more to the point you should be looking into Post Redirect Get (PRG) also known as "Get after Post."

http://www.theserverside.com/patterns/thread.tss?thread_id=20936

这篇关于固化"后退按钮布鲁斯"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆