且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-04 20:09:28

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



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


I am trying to get my head around PHP and using the CodeIgniter Framework. I have recently been following this tutorial:

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

I went through it very methodically and was careful to ensure I made no errors in writing the code. However when I run my project in a browser I get this error after I attempt to submit my login details.

Not Found The requested URL /LoginTut/login/VerifyLogin was not found on this server.

I ran into a similar problem not to long ago when I was following another tutorial and could not figure it out so abandoned the project.

This is how I configured my CI

routes.php

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

config.php

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

I left this blank.

$config['index_page'] = '';

I've also ensure I am loading all relevant libraries and helpers.

autoload.php

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

Below are all of my PHP files..

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

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;
    }
}

}

The user should be able to successfully log in and end up at the home.php controller which loads the home_view.php file.

This is the Post action of the login form

http://localhost/LoginTut/login/VerifyLogin

I would really appreciate any help relating to this issue, I've been struggling with it for days now and It's starting to really annoy me. I've a feeling It's something very small going wrong within my code. I just don't have enough knowledge to figure it out.

NOTE: I read in a forum it could be something to do with a .htaccess problem, but I've no idea what that is!

.htaccess file

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

Many thanks.

I answered your other post as well, but just to reiterate:

You named your controller "verify_login.php" but your class is named "VerifyLogin". In Codeigniter your class needs to be named the same as your file, just with capital letters. In order for that page to work correctly you should either rename your controller file to "verifyLogin.php" or rename your class to "Verify_Login".