简单登录系统使用CodeIgniter在登录时返回404 [英] Simple Login System using CodeIgniter returning 404 on login

查看:252
本文介绍了简单登录系统使用CodeIgniter在登录时返回404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的头脑 PHP 并使用 CodeIgniter 框架。我最近一直在遵循本教程:



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



我经历了非常有条不紊的仔细确保我在编写代码时没有犯错误。但是当我在浏览器中运行我的项目时,我在尝试提交我的登录详细信息后收到此错误。



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



我遇到了类似的问题,不久以前,当我正在追踪另一个教程,数字就这么放弃了这个项目。



这是我如何配置我的CI



routes.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用户扩展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 - >限制(1);

$ query = $ this - > db - >得到();

if($ query - > num_rows()== 1){
retrun $ query-> result();
}
else {
return false;
}
}

}



login_view.php

 <?php echo form_open('VerifyLogin') ; > 
< label for =username>用户名:< / label>
< input type =textsize =20id =usernamename =username/>
< br />
< label for =password>密码:< / label>
< input type =passwordsize =20id =passowrdname =password/>
< br />
< input type =submitvalue =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){
//验证失败,用户重定向到登录页面...
$ this - > load-> view('login_view');
}
else {
//登录成功。转到会员专区
redirect('home','refresh');
}
}

function check_database($ password){
//字段验证成功。验证对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','无效的用户名或密码');
return false;
}
}

}



用户应该能够成功登录并在 home.php 控制器上加载 home_view.php 文件。



这是登录表单的Post操作

 code> http:// localhost / LoginTut / login / VerifyLogin 

关于这个问题的帮助,我一直在努力与它几天了,它开始真的烦我。我有一种感觉这是一个非常小的错误在我的代码。我只是没有足够的知识来计算出来。



注意:我在一个论坛读到它可能是一个.htaccess问题,不知道是什么!



.htaccess文件

 < IfModule mod_rewrite.c> 
RewriteEngine On
#Removes用户访问系统文件夹。
#另外,这将允许您创建一个System.php控制器,
#previously这是不可能的。
#'system'可以替换,如果你已经重命名您的系统文件夹。
RewriteCond%{REQUEST_URI} ^ system。*
RewriteRule ^(。*)$ index.php?/ $ 1 [L]

#当您的应用程序文件夹不在系统文件夹
#此代码段阻止用户访问应用程序文件夹
#提交者:Fabdrol
#Rename'application'到您的应用程序文件夹名称。
RewriteCond%{REQUEST_URI} ^ LoginTut。*
RewriteRule ^(。*)$ index.php?/ $ 1 [L]

#检查用户是否尝试访问一个有效的文件,
#s如一个图像或CSS文档,如果这不是真的,它发送
#request到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 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".

这篇关于简单登录系统使用CodeIgniter在登录时返回404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆