使用CodeIgniter的简单登录系统在登录时返回404


Simple Login System using CodeIgniter returning 404 on login

我试图绕过PHP并使用CodeIgniter框架。我最近一直在关注本教程:

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

我非常有条不紊地完成了它,并小心翼翼地确保我在编写代码时没有出错。但是,当我在浏览器中运行我的项目时,我在尝试提交登录详细信息后收到此错误。

未找到在此服务器上找不到请求的 URL/LoginTut/login/VerifyLogin。

不久前,当我按照另一个教程学习时,我遇到了类似的问题,无法弄清楚,所以放弃了这个项目。

这就是我配置 CI 的方式

路线.php

$route['default_controller'] = "login";

配置.php

$config['base_url'] = 'http://localhost/LoginTut/';

我把它留空了。

$config['index_page'] = '';

我还确保我正在加载所有相关的库和帮助程序。

自动加载.php

$autoload['libraries'] = array('database', 'session'); $autoload['helper'] = array('url');

以下是我所有的PHP文件。

用户.php

class User extends CI_Model{
function login($username, $password){
    $this -> db -> select('id, username, password');
    $this -> db -> from ('users');
    $this -> db -> where ('username', $username);
    $this -> db -> where ('password', md5($password));
    $this -> db -> limit(1);
    $query = $this  -> db -> get();
    if($query -> num_rows() == 1){
        retrun $query->result();
    }
    else{
        return false;
    }
}

}

login_view.php

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

登录.php

class Login extends CI_Controller{
    function _construct(){
        parent::_construct();
    }
    function index(){
        $this->load->helper(array('form'));
        $this->load->view('login_view');
    }
}

verify_login.php

class VerifyLogin extends CI_Controller{
function _construct(){
    parent::_construct();
    $this->load->model('user', '', TRUE);
}
function index(){
    $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){
        //Validation failed, user redirected to login page...
        $this->load->view('login_view');
    }
    else{
        //Login success. Go to members only area
        redirect('home', 'refresh');
    }
}
function check_database($password){
    //Field validation succeeded. Validate against db
    $username = $this->input->post('username');
    //query db
    $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;
    }
}

}

用户应该能够成功登录并最终进入加载home_view.php文件的home.php控制器。

这是登录表单的发布操作

http://localhost/LoginTut/login/VerifyLogin

我真的很感激与这个问题有关的任何帮助,我已经为此苦苦挣扎了好几天,它开始真正惹恼我了。我有一种感觉,这是我的代码中出现非常小的问题。我只是没有足够的知识来弄清楚。

注意:我在论坛上读到它可能与.htaccess问题有关,但我不知道那是什么!

.htaccess 文件

<IfModule mod_rewrite.c>
RewriteEngine On
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^LoginTut.*
RewriteRule ^(.*)$ index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
 ErrorDocument 404 index.php
</IfModule>

非常感谢。

我也

回答了你的其他帖子,但只是重申一下:

您将控制器命名为"verify_login.php",但您的类名为"验证登录"。在 Codeigniter 中,您的类需要与您的文件命名相同,只是使用大写字母。为了使该页面正常工作,您应该将控制器文件重命名为"verifyLogin.php"或将类重命名为"Verify_Login"。

关闭重写一段时间。首先使用普通的CodeIgniter URL修复此问题:

CodeIgniter URL 的工作方式如下:

<base_url>/<index-page>/<controller>/<methodOfController>/<parameters>

当您请求.../login/VerifyLogin时,这将不起作用,因为登录验证登录都是控制器。CodeIgniter 会"认为"你想在控制器类 login 中调用方法 VerifyLogin((。您应该改为调用.../VerifyLogin/index

你把$config['index_page']留空了。在 CodeIgniter 中,您不能直接在控制器上调用方法;你必须让CodeIgniter为你做这件事。这意味着 CodeIgniter 必须首先运行,然后解析 URL 并将请求委托给正确的控制器和方法。这就是配置 *index_page* 的用途 - 启动 CodeIgniter。您必须通过此文件运行所有请求。

有一个索引.php包含在CodeIgniter中(它以行require_once BASEPATH.'core/CodeIgniter.php';结尾(。使用这个作为index_page。

完成此操作后,完整的URL将是:

http://localhost/LoginTut/index.php/VerifyLogin/index/<parameters>