1

如何防止在 CodeIgniter 中刷新表单页面?

如果我使用重定向——一切正常,但我可以直接访问 site.com/update/success 页面。如何防止直接访问成功页面(仅来自 site.com/update/)?

控制器更新.php

public function index() {
   if($this->form_validation->run() == FALSE) {
      $data['error'] = 'Something wrong';
      $this->load->view('update', $data);
   } else {
      redirect('/update/success');
   }
}

public function success() {
   $message = 'Your profile has been successfully updated';
   $this->load->view($message);
}
4

3 回答 3

4

您可以在 index() 函数中的 flashdata 中设置一个标记,然后在 success() 方法中检查该标记。

class Update extends CI_Controller {

    property $token;

    public function __construct()
    {
        $this->load->library('session');
    }

    public function index() {
       if($this->form_validation->run() == FALSE) {
          $data['error'] = 'Something wrong';
          $this->load->view('update', $data);
       } else {
          $this->session->set_flashdata('update_token', time());
          redirect('/update/success');
       }
    }

    public function success() {

        // Make sure this request came from the index() method...
        if( ! $this->session->flashdata('update_token'))
        {
            redirect();
        }

       $message = 'Your profile has been successfully updated';
       $this->load->view($message);
    }
}
于 2013-01-31T00:37:59.533 回答
0

前段时间我使用了codeigniter,所以我不确定。

也许您可以将 succes 函数设为私有,然后像这样在 else 中调用它:

    public function index() {
        if($this->form_validation->run() == FALSE) {
            $data['error'] = 'Something wrong';
            $this->load->view('update', $data);
        } else {
            $this->success();
        }
   }



    private function success() {
       $message = 'Your profile has been successfully updated';
       $this->load->view($message);
    }
于 2013-01-30T23:59:48.003 回答
0
public function index() {
   if($this->form_validation->run() == FALSE) {
      $data['error'] = 'Something wrong';
      $this->load->view('update', $data);
   } else {
      // create success session/cookie
      $this->_success()
   }
}

public function _success() {
   // destroy success session when called
    // checks success session if existing if not the page has been refreshed redirect
   $message = 'Your profile has been successfully updated';
   echo $this->nocache();
   $this->load->view($message);
}

public function _nocache()
{
 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
 header("Cache-Control: no-store, no-cache, must-revalidate"); 
 header("Cache-Control: post-check=0, pre-check=0", false);
 header("Pragma: no-cache");
}

您可以添加“_”下划线,使其无法通过 url 访问,并在标题中添加 no cache,这样当他们单击返回或再次访问相同的 url 时,页面不会在浏览器上保持缓存。

代码未测试

于 2013-01-31T00:06:23.047 回答