【问题标题】:Codeigniter: form_open redirects to blank pageCodeigniter:form_open 重定向到空白页面
【发布时间】:2016-03-25 21:33:00
【问题描述】:

我一直在关注这个使用 CodeIgniter 进行简单登录的教程

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

每当我点击登录按钮时,我都会被重定向到一个空白页面,而不是进入“verifylogin”控制器。我试图将 form_open('verifylogin') 更改为 form action="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;

        }

    }

}

?>

【问题讨论】:

  • 添加error_reporting(E_ALL)。我认为你有一些错误。
  • 对不起,我在哪里添加它?
  • 您使用的 codeigniter 版本可能与教程不同,因为 codeigniter 3 有很多变化。
  • 您还需要加载安全助手以使用codeigniter.com/user_guide/helpers/security_helper.html表单中的xss_clean
  • 在视图上的操作确保匹配控制器名称form_open(verifylogin')class Verifylogin extends CI_Controller {} 文件名Verifylogin.php

标签: php codeigniter


【解决方案1】:

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

但在使用 base_url() 之前,请确保您已调用 $this->load->helper('url');在您的关联控制器中。

【讨论】:

    【解决方案2】:

    改变这个:

    $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');
    

    到:

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

    xss_clean 可能是导致问题的原因,希望对您有所帮助。还要确保如果您没有配置您的.htaccess 文件以从您需要使用form_open('index.php/verify_login') 的url 中删除index.php

    【讨论】:

      【解决方案3】:

      您是否加载了 url 帮助程序??

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

      请检查并告诉我

      【讨论】:

        猜你喜欢
        • 2018-09-30
        • 1970-01-01
        • 1970-01-01
        • 2020-10-07
        • 2016-10-20
        • 2017-09-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多