Simple Login System using CodeIgniter returning 404 on login

跟風遠走 提交于 2019-12-13 12:38:02

问题


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.


回答1:


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




回答2:


Turn off rewrite for a while. First fix this with the normal CodeIgniter URLs:

CodeIgniter URLs work like this:

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

When you requested .../login/VerifyLogin that will not work because both login and VerifyLogin are controllers. CodeIgniter will "think" that you want to call the method VerifyLogin() in the controller class login. You should instead call .../VerifyLogin/index.

You left $config['index_page'] empty. In CodeIgniter you cannot call methods on controllers directly; you have do let CodeIgniter do it for you. This means CodeIgniter has to run first and will then parse the URL and delegate the request to the correct controller and method. That is what the config *index_page* is for - to start CodeIgniter. You have to run all requests through this file.

There is an index.php included in CodeIgniter (it ends with the line require_once BASEPATH.'core/CodeIgniter.php';). Use this one as index_page.

When you've done this your complete URL will be this:

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


来源:https://stackoverflow.com/questions/14940118/simple-login-system-using-codeigniter-returning-404-on-login

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!