2014-10-11 90 views
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> 

回答

0

你要做的就像下面的步驟 在模型中你必須創建方法來檢查用戶名和密碼,在我的例子中,我將調用該方法有效($用戶名,$密碼); 如果匹配db,則此方法將返回true,否則返回false。

在您的控制器中。

public function login() 

    { 
    // check if button is clicked by 
    if(isset($_POST['loginButton'])){ 

// check if username and password is correct 

if($model->valid($this->input>post('username'),$this->input>post('password')){ 

//redirect to admin page 

}else{ 

//load login form template 
} 
      }else{ 
// load your login form template here 

} 

} 

很好,這裏的要點是,刪除後從瀏覽器提交您必須將用戶重定向到另一個頁面,上面的例子。

注意:你的問題是,如果你在控制器的末尾顯示模板,你必須在if語句上顯示模板。