duane9322 2014-08-20 13:49
浏览 80
已采纳

如果上一页具有条件重定向命令,则返回按钮行为

In my PHP form structure

page 1 : has form

page 2 : executes conditional redirections (to page 1 or to page 3)

page 3 : record form into DB, inform the visitor about successful operation and thank to the visitor

sample simplified code

page 1

html form (action is POST to page 2)

page 2

if ( session variable value is 'success' )
{ redirect to homepage;
die; }

if form is not correctly filled redirect to page 1
if form is correctly filled redirect to page 3

page 3

insert form into MySQL database;
echo 'thanks visitor, your comments has been added';
set session variable to 'success';

note: BACK button below means the browser's back button (not a custom link)

if I hit BACK button on page 3 after successfull form submission ( page orders page 1 -> page 2 -> page3 ) I observed that browser (Chrome for the time being) page goes to page 1. ( so after hittng back button the page order is page 3 -> page 1 )

I couldn't achieve page 3 -> page 2 -> some page (I am 99.9% sure that my codes for this trials are correct and valid for the purpose)

So I ask that what is the behaviour of BACK button in this situation? Why my PHP codes were not read by the server and applied by the browser (I mean PHP codes of page 2. Because order was page 2 -> page 3 so by hitting back, I normally expect codes of page 2 is to be read again)

What I exactly can not understand is

  • I set session variable to 'success' in page 3.
  • I say in page 2 that if session variable is 'success' then go to homepage
  • page 2 is the just previous page of page 3

So why I can't arrive to homepage after hitting back button while I am on page 3?

  • 写回答

2条回答 默认 最新

  • dongzai5181 2014-08-20 14:27
    关注

    When you submit your form to Page 2, and Page2 processes your data then redirects to either page1 or page3; Page2 redirects using headers and interrupts the loading of page2, and your browser proceeds to load page3 instead.

    The interruption causes page2 to have never been loaded, so the browser does not put that in your history. This is the standard protocol of serverside redirection. In fact the majority of sites on the internet relies on this protocol to silently redirect users to the correct page, otherwise you would get a bunch of useless redirection pages in your browser's history.

    Now my question to you is: why would you want "back" to go to page2?

    The form submission has already been processed, and hitting back to re-process is bad UX, since if a user submitted good data to page2 and user gets page3, but wants to go back to page1, the user would have to go back to page2, which should prompt the user whether to resubmit the data or not, most users would not know how to react to this and pressing "yes" to this will result in either duplicate data, or redirecting user to page1 with error messages, depending your logic in page2.

    If you want to have the ability to resubmit form data without typing everything out again, you should actually move page3 into page2, and redirect back to page1 only if form submission fails. Then to resubmit, just refresh page to get that resubmission prompt and click "yes".

    The moral of the story is to not over complicate your redirections, KISS, just submit form and get a result, no need to: set a session variable, then redirect, then check if session variable is set...etc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog