簡體   English   中英

Codeigniter中的表單驗證

[英]form-validation in codeigniter

<?php echo validation_errors(); ?>
<?php echo form_open('verifylogin'); ?>
  <label for="username">Username:</label>
  <input type="text" size="20" id="username" name="username"/>
  <br/>
  <label for="password">Password:</label>
  <input type="password" size="20" id="password" name="password"/>
  <br/>
  <input type="submit" value="Login"/>
</form>

我使用該代碼來驗證填寫的表單,但是提交后瀏覽器什么也沒有顯示。 我的瀏覽器的網址停留在http://example.com/index.php/verifylogin

verifylogin.php是這樣定義的

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class VerifyLogin extends CI_Controller {

 function __construct()
 {
   parent::__construct();
   $this->load->model('user','',TRUE);
 }

 function index()
 {
   //This method will have the credentials validation
   $this->load->library('form_validation');

   $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
   $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');

   if($this->form_validation->run() == FALSE)
   {
     //Field validation failed.&nbsp; User redirected to login page
     $this->load->view('login_view');
   }
   else
   {
     //Go to private area
     redirect('home', 'refresh');
   }

 }

 function check_database($password)
 {
   //Field validation succeeded.&nbsp; Validate against database
   $username = $this->input->post('username');

   //query the database
   $result = $this->user->login($username, $password);

   if($result)
   {
     $sess_array = array();
     foreach($result as $row)
     {
       $sess_array = array(
         'id' => $row->id,
         'username' => $row->username
       );
       $this->session->set_userdata('logged_in', $sess_array);
     }
     return TRUE;
   }
   else
   {
     $this->form_validation->set_message('check_database', 'Invalid username or password');
     return false;
   }
 }
}
?>
class Auth extends CI_Controller{

    public function __construct(){parent::__construct();}
     /**
     * Login Form
     *
     * $route['login'] = 'auth/login_form';
     *
     */
    public function login_form(){
         $this->load->view('login_form');
    }
    /**
     * Login Validation
     *
     * $route['login/check'] = 'auth/login_check';
     * Point your form to login/check
     */
    public function login_check()
    {
        if($this->form_validation->run() == TRUE)
        {
            //form validates...
        }else
        {
             //no redirect! just show for again!
             $this->login_form();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM