du7133 2014-09-03 23:47
浏览 44

防止用户在提交后返回PHP表单[关闭]

I have 2 PHP forms. If a user submits the first form he should be redirected to the second form.

I would like to prevent users from going back to the first form after submitting it. As of now, after I submit the first form, it will redirect me to the second form but if I press the back button in the browser, I can see all the data I inserted in the first form.

(I used header ('Location: form2.php') at the end of my PHP file to redirect to second form and it works properly. I searched a lot about preventing from resubmitting the form but still I could not solve my problem.

  • 写回答

2条回答 默认 最新

  • dongxian3574 2014-09-04 00:34
    关注

    I would personally have the form be on the same page (aka form.php), but control the output on the page based on variables that you pass through the form.

    On the first form, you could put a hidden var that tells it which form has been completed.

    ex: <input type="hidden" name="task" value="form1" />

    this way, when you pass the variables to the second form, you can have control over the variables passed and provide the propper output... ex.

    <?php
    if ($_POST['task'] == 'form1') {
      // show form2
    } else {
      // show form1
    }
    <?
    

    Then again, this is just based on what you described. Depending on what the forms are for might change the solution. Also, if you want to have the users not be able to go back to form1 until they have finished the form, then you would want to save the submitted form variables.

    <?php
    session_start();
    if ($_POST['task'] == 'form1') {
      $_SESSION['blah'] = $_POST['blah'];
      $_SESSION['form1'] = true;
    }
    <? 
    

    At this point, the check to see what form they were on would be similar to:

    <?php
    if ($_SESSION['form1']) {
      // show form 2
    }
    <? 
    

    Just a warning, I haven't debugged any of this code so it might not work as-is.

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程