0
我正在使用Codeigniter。我有一個名爲login的方法的控制器,該方法接受表單數據並檢查用戶是否存在?,一旦成功,就加載另一個視圖。但是當我點擊瀏覽器中的刷新按鈕時,我的控制器中的方法重新執行,我想阻止重新提交表單數據。請幫助我。也請告訴我它應該如何正常運行,是否應該防止重新提交表單並保持瘋狂,或者是否應該重定向到登錄屏幕。Codeigniter避免重新提交表單數據
這裏是代碼:
登錄控制器
<?php
class Login extends CI_Controller {
/**
*
* load the Models
*/
function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->model('User');
// $this->session->set_flashdata('success','data posted');
$this->load->library('session');
$this->load->helper('url');
}
public function index() {
$this->load->view('login');
}
/**
*
* Log in a user
*/
public function login() {
$this->form_validation->set_rules(array(
array(
'field' => 'username',
'label' => 'username',
'rules' => 'required|is_numeric|max_length[1]|min_length[1]',
),
array(
'field' => 'password',
'label' => 'password',
'rules' => 'required|is_numeric|max_length[1]|min_length[1]',
),
));
$this->form_validation->set_error_delimiters('<div class="alert alert-error">', '</div>');
//GETTING ID AND PASSWORD FROM THE FORM
$id = $this->input->post('username') . "<br>";
$password = $this->input->post('password') . "<br>";
//check if user exists
$user = new User();
$user_exists = $user->user_exists($id, $password);
//validate the user input
$validated = $this->form_validation->run();
if (!$validated) {
$this->load->view('login');
} else {
if ($user_exists) {
//use php function to pickup the current year from the pc
//set your min to 01-01-current year and your max to 31-12-current year
$min = strtotime(date('Y') . "-01-01");
$max = strtotime(date('Y') . "-12-31");
$date = rand($min, $max);
$dec2 = strtotime('2014-12-02');
$randomDate = date('Y-m-d', $date);
echo "random date:" . $randomDate . "<br>";
$date_timestamp = strtotime($randomDate);
if ($date_timestamp >= $dec2) {
$is_updated = $user->is_holiday_updated();
if (!$is_updated) {
// $user->update_national_holidays();
echo "holiday table updated";
} else {
echo "already updated the table";
}
} else {
echo "no update required";
}
$this->load->view('national_holiday_screen');
} else {
$this->load->view('login');
}
}
}
}
這裏查看:
<h2> National Holiday Screen </h2>