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

标签 php codeigniter login http-status-code-404

我正在努力解决PHP并使用 CodeIgniter框架。我最近一直在关注本教程:

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

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

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


不久前,我在学习另一个教程时遇到了类似的问题,但无法弄清楚,因此放弃了该项目。

这就是我配置 CI 的方式

路线.php
$route['default_controller'] = "login";
config.php
$config['base_url'] = 'http://localhost/LoginTut/';
我把这个留空了。
$config['index_page'] = '';
我还确保我正在加载所有相关的库和助手。

autoload.php
$autoload['libraries'] = array('database', 'session');$autoload['helper'] = array('url');
下面是我所有的 PHP 文件..

user.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>

login.php
class Login extends CI_Controller{
    function _construct(){
        parent::_construct();
    }

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

验证登录.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.php加载 home_view.php 的 Controller 文件。

这是登录表单的 Post 操作
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>

非常感谢。

最佳答案

我也回答了你的另一篇文章,但只是重申:

您将 Controller 命名为“verify_login.php”,但您的类名为“VerifyLogin”。在 Codeigniter 中,您的类需要与您的文件命名相同,只是使用大写字母。为了使该页面正常工作,您应该将 Controller 文件重命名为“verifyLogin.php”或将您的类重命名为“Verify_Login”。

关于php - 使用 CodeIgniter 的简单登录系统在登录时返回 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14940118/

相关文章:

php - Codeigniter 插入代码不起作用

mysql - 在 codeigniter 迁移中向列添加注释

login - Hybris hMC 登录配置

php - 使用 php 文件更新数据库中的内容

javascript - Foursquare - 如何使用 foursquare API 获取超过 100 个 field ?

php - stream_socket_sendto(): Connection refused

php - 使用Composer安装时出现CodeIgniter 4问题

PHP,用什么代替create_function()?

php - 仅当请求来自另一个特定网站时才允许登录网站

tsql - SQL Server 2000传输登录名和关联的服务器角色以及对特定数据库的访问