如何防止CodeIgniter中的刷新表单页面?
如果我使用重定向 - 一切都很好,但我可以直接申请页面site.com/update/success。 如何阻止直接访问成功页面(仅限于site.com/update/)?
Controller 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);
}
答案 0 :(得分:3)
您可以在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);
}
}
答案 1 :(得分:0)
不久之前我使用了codeigniter,所以我不确定。
也许你可以将成功函数设为私有,只需在其他地方调用它:
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);
}
答案 2 :(得分: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无法访问它,并为标题添加无缓存,这样当页面点击或再次访问同一个网址时,页面就不会在浏览器上保留缓存。 / p>
代码未经过测试