Codeigniter避免重新提交表单数据

时间:2014-10-11 18:36:49

标签: php codeigniter

我正在与Codeigniter合作。我有一个名为login的方法的控制器,它接受表单数据并检查用户是否存在?,成功后加载另一个视图。但是当我单击浏览器中的刷新按钮时,我的控制器中的方法重新执行,我想阻止重新提交表单数据。请帮助我。另外请告诉我它应该如何正常运行,它是否应该只是阻止重新提交表格并保持疯狂,还是应该重定向到登录屏幕。

这是代码:

登录控制器

<?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>

1 个答案:

答案 0 :(得分:0)

您必须执行以下步骤 在你的模型中你必须创建方法来检查用户名和密码,在我的例子中,我将调用该方法有效($ username,$ password); 如果匹配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语句在控制器的末尾显示模板,您必须在if语句上显示模板。