Codeigniter:form_open重定向到空白页面

时间:2015-12-19 06:36:42

标签: php codeigniter

我一直在使用CodeIgniter

进行简单登录的本教程

http://www.iluv2code.com/login-with-codeigniter-php.html

每当我点击登录按钮时,我都会被重定向到空白页面,而不是转到“verifylogin”控制器。我试图改变form_open('verifylogin')以形成action =“verifylogin”,以确保它到达verifylogin。它到达verifylogin但似乎无法做正确的功能。为什么会这样?为什么在提交表单时我被重定向到空白页面? 谢谢!

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

class Login extends CI_Controller {

function __construct() {
   parent::__construct();
}

function index() {

  $this->load->helper(array('form'));
  $this->load->view('login_view');
}

}

?>

查看

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Simple Login with CodeIgniter</title>
    </head>
    <body>
        <h1>Simple Login with CodeIgniter</h1>
        <?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="passowrd" name="password"/>
        <br/>
        <input type="submit" value="Login"/>
        </form>
    </body>
</html>

控制器

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

        }

    }

}

?>

2 个答案:

答案 0 :(得分:2)

有时在你的页面前面有index.php前缀,以避免在使用form_open()调用页面之前使用base_url()函数; 喜欢 : echo form_open(base_url()."contact-us");

但在使用base_url()之前,请确保已调用$ this-&gt; load-&gt; helper('url');在你的助理控制员。

答案 1 :(得分:0)

你加载了url helper ??

$this->load->helper(array('url', 'form'));

检查并告诉我