Codeigniter,在表单提交后阻止浏览器的后退按钮

时间:2022-09-25 16:13:44

I have a Controller in Codeigniter like following :

我在Codeigniter中有一个Controller,如下所示:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Charity extends MY_Controller {
    public function index()
    {
        if($this->input->method() === 'post'){
            $data['name'] =  $this->input->post('name');
            $this->load->view('review_page', $data); 
        }else{
            $this->load->view('charity');
        }
    }
}

the views are :

意见是:

charity.php 
<form method="post" action="/">
<input name="name" type="text"/>
<input value="send" type="submit"/>
</form>

review_page.php
<h1><?= echo $name; ?></h1>

When I submit the form I get the review_page view loaded but if I click on the browser's back button I come back to the form. Is there a why to make the form page expired after it is submitted so that the back button does not show it?

当我提交表单时,我会加载review_page视图但是如果我点击浏览器的后退按钮,我会回到表单。是否有原因使表单页面在提交后过期,以便后退按钮不显示?

Thanks

谢谢

2 个解决方案

#1


1  

Back button does not work because you never actually redirect to a different URL. You need to build your code so that it redirects. I would build another method that loads the review page like so

后退按钮不起作用,因为您实际上从未实际重定向到其他URL。您需要构建代码以便重定向。我会构建另一种加载评论页面的方法

public function index() {
    $this->load->view('charity');
} 

public function reviews(){
  if($this->input->method() === 'post'){
      $data['name'] =  $this->input->post('name');
      $this->load->view('review_page', $data); 
  } else {
      redirect('charity');
  }
}

And your html

而你的HTML

<form method="post" action="reviews">

#2


0  

Maybe this can help:

也许这可以帮助:

at the top of charity.php add this code:

在charity.php的顶部添加以下代码:

 <?php
   session_start();
   if ( isset($_SESSION['isSubmit']) and $_SESSION['isSubmit'] == 'ok' )
   {
     echo 'page is expired: form already submit !';
     session_destroy();
   }
 ?>

at the top of review_page.php add this code:

在review_page.php的顶部添加以下代码:

 <?php
   session_start();
   $_SESSION['isSubmit'] = 'ok' ;
 ?>

#1


1  

Back button does not work because you never actually redirect to a different URL. You need to build your code so that it redirects. I would build another method that loads the review page like so

后退按钮不起作用,因为您实际上从未实际重定向到其他URL。您需要构建代码以便重定向。我会构建另一种加载评论页面的方法

public function index() {
    $this->load->view('charity');
} 

public function reviews(){
  if($this->input->method() === 'post'){
      $data['name'] =  $this->input->post('name');
      $this->load->view('review_page', $data); 
  } else {
      redirect('charity');
  }
}

And your html

而你的HTML

<form method="post" action="reviews">

#2


0  

Maybe this can help:

也许这可以帮助:

at the top of charity.php add this code:

在charity.php的顶部添加以下代码:

 <?php
   session_start();
   if ( isset($_SESSION['isSubmit']) and $_SESSION['isSubmit'] == 'ok' )
   {
     echo 'page is expired: form already submit !';
     session_destroy();
   }
 ?>

at the top of review_page.php add this code:

在review_page.php的顶部添加以下代码:

 <?php
   session_start();
   $_SESSION['isSubmit'] = 'ok' ;
 ?>